60

我刚刚更新到 Django v1.8,并在更新我的项目之前测试了我的本地设置,并且我收到了一个我以前从未见过的弃用警告,对我来说也没有任何意义。我可能只是忽略了某些东西或误解了文档。

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:6: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Difficulty doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Difficulty(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:21: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Zone doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Zone(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:49: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Boss doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Boss(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:79: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Item doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Item(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:14: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Category doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Category(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:36: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Comment doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Comment(ScoreMixin, ProfileMixin, models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:64: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Forum doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Forum(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:88: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Post doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Post(ScoreMixin, ProfileMixin, models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:119: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.CommentPoint doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class CommentPoint(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:127: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.TopicPoint doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class TopicPoint(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/auctionhouse/models.py:10: RemovedInDjango19Warning: Model class ankylosguild.apps.auctionhouse.models.Auction doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Auction(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/auctionhouse/models.py:83: RemovedInDjango19Warning: Model class ankylosguild.apps.auctionhouse.models.Bid doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Bid(models.Model):

现在这对我提出了 3 个问题。

  1. 根据文档Options.app_label除非模型在应用程序模块之外,否则不是必需的,在我的情况下,它不是。其次,无论如何,这种行为在 1.7 中已被弃用,那么为什么它甚至是一个问题呢?
  2. 应用程序都在 INSTALLED_APPS 元组中,所以肯定不是这样吗?
  3. 如果所有内容都在 INSTALLED_APPS 元组中,为什么在调用应用程序之前不加载应用程序?

如果我确实做错了什么,那么正确的做法是什么,因为文档并没有真正弄清楚导致这个问题的原因或如何纠正它。

4

13 回答 13

51

如警告中所述,这会发生:

  • 当您使用不在INSTALLED_APPS;中的模型时
  • 或者当您在加载应用程序之前使用模型时。

由于您确实在设置中引用了该应用程序INSTALLED_APPS,因此您很可能在应用程序初始化之前使用了模型。

通常,当您有from .models import SomeModels一个apps.py早期信号(例如post_migrate)时,就会发生这种情况。建议使用AppConfig.get_model(),而不是在这里以经典方式引用您的模型。检查您的apps.py文件是否有任何模型导入,并使用此 api 替换它们。

例如,而不是:

# apps.py

from django.apps import AppConfig
from .models import MyModel

def do_stuff(sender, **kwargs):
    MyModel.objects.get() # etc...

class MyAppConfig(AppConfig):
    name = 'src.my_app_label'

    def ready(self):
        post_migrate.connect(do_stuff, sender=self)

做这个 :

# apps.py

from django.apps import AppConfig

def do_stuff(sender, **kwargs):
    mymodel = sender.get_model('MyModel')
    mymodel.objects.get() # etc...

class MyAppConfig(AppConfig):
    name = 'src.my_app_label'

    def ready(self):
        post_migrate.connect(do_stuff, sender=self)

请注意,此强制措施是在错误#21719中引入的。

于 2015-04-17T15:14:57.343 回答
51

类似的错误。就我而言,错误是:

RemovedInDjango19Warning: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Site(models.Model):

我的解决方案是:

添加'django.contrib.sites'INSTALLED_APPS

于 2015-07-12T17:01:28.147 回答
23

我怀疑只有极少数人会看到这个错误,因为这将是由与我相同的事情引起的,但如果它对其他人有帮助,似乎值得添加这个答案!

在一次运行测试时,我突然看到了很多这些错误——结果我不小心在__init__.py我的 Django 项目的顶层创建了一个,而它本来应该在一个子目录中。发生这种情况的线索是错误,例如:

/home/mark/mystupiddjangoproject/alerts/models.py:15: RemovedInDjango19Warning: Model class mystupiddjangoproject.alerts.models.Alert doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Alert(models.Model):

...mystupiddjangoproject在完全限定的模型名称中包含项目所在目录的名称 ( ),它应该是:alerts.models.Alert.

为了解决这个问题,我只需要这样做:

rm __init__.py
rm __init__.pyc
于 2016-05-08T10:31:57.550 回答
10

我遇到了类似的错误,但不是抱怨我的应用程序中的模型,而是抱怨contrib包中的模型。例如:

C:\Program Files\Python 2.7\lib\site-packages\django\contrib\sessions\models.py:27: RemovedInDjango19Warning: Model class django.contrib.sessions.models.Session 没有声明明确的 app_label '不在 INSTALLED_APPS 中的应用程序中,否则在加载其应用程序之前已导入。Django 1.9 将不再支持此功能。类会话(模型。模型):

INSTALLED_APPS这是由于 中的属性排序错误造成的settings.py。我settings.py最初包含:

INSTALLED_APPS = (
    'my_app_1',
    'my_app_2',
    'my_app_3',
    'bootstrap_admin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',
    'social.apps.django_app.default',
    'mathfilters',
    'rest_framework',
)

my_app_*使用contrib包中的模型。该错误是由在声明模型之前使用模型引起的(即在使用它们之前Django应该了解包含这些模型的应用程序)。

为了解决这个问题,需要更改应用程序的声明顺序。具体来说,所有 Django 应用程序都应该在用户定义的应用程序之前。就我而言,正确的INSTALLED_APPS看起来像:

INSTALLED_APPS = (
    'bootstrap_admin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.staticfiles',
    'social.apps.django_app.default',
    'mathfilters',
    'rest_framework',
    'my_app_1',
    'my_app_2',
    'my_app_3',
)

现在我知道这可能不会直接回答您的问题,但它回答了一个相关问题,并且由于这是粘贴错误时在 Google 上显示的唯一 SO 链接,所以我在这里回答了它。

但是,我相信类似的情况会导致您的问题:

确保在应用程序使用它们之前声明“依赖”应用程序!这些错误并没有真正指定哪个应用程序正在使用模型,因此您必须将包含它提到的模型的应用程序一个接一个地推送到顶部,直到错误消失。

于 2015-05-30T09:32:15.620 回答
8

使用 app_label 属性将元类添加到您的模型中。

class Meta:
    app_label = 'app_model_belongs_to'

希望这有效!

编辑: 这样做的原因通常是模型存在于标准位置之外。

有关更多信息,请参阅: https ://docs.djangoproject.com/en/1.8/ref/models/options/#app-label

于 2015-04-16T16:55:27.430 回答
5

我也遇到了这个问题,这与我从应用程序加载 signals.py 模块的方式有关。

正如您现在所见,信号和模型之间存在循环导入问题是很常见的,通常从应用程序__init__.py文件或文件底部导入它们models.py以避免它们。

好吧,我正在使用该__init__.py方法,只需import signals语句移到文件底部即可models.py解决问题

希望这对其他人有帮助!

于 2015-11-18T08:43:40.430 回答
5

Django 1.9 处理此问题并在管理员中为您的应用程序命名的方法是执行以下操作:

在您的应用程序中添加一个名为的文件apps.py并将以下内容添加到其中:

#apps.py
from django.apps import AppConfig


class YourAppNameAppConfig(AppConfig):
    name = 'yourappname'
    verbose_name = 'Your App Name Looking Right'

然后,在您的应用程序__init__.py文件中,添加以下内容:

#__init__.py    
default_app_config = 'youappname.apps.YourAppNameAppConfig'
于 2015-10-26T19:36:40.867 回答
5

将 Django 从 1.8 升级到 1.9.1 后我遇到了这个问题:

运行时错误在 /

模型类 blog.models.BlogCategory 没有明确声明 app_label 并且不在 INSTALLED_APPS 中的应用程序中。

这有助于解决:

在博客/models.py 中:

class BlogCategory(models.Model):
    some vars & methods

    class Meta:
        app_label = 'BlogCategory'

这是100%的工作。

于 2016-01-11T14:54:48.203 回答
4

我有同样的问题。我在 django.db.models.base.py:line82 中放置了一个断点,并尝试找出导致此警告消息的原因。

# Look for an application configuration to attach the model to.
app_config = apps.get_containing_app_config(module)

基本上,如果此时您的应用程序不存在,您会收到该警告。我意识到我的问题是我有一个第三方框架(在我的例子中是 haystack),它试图导入我的一个自定义模型。

也许在您的自定义应用程序和您的第三方包引用您的自定义应用程序之前,您在 INSTALLED_APPS 中还列出了第三方包?如果您也使用 Django rest 框架之类的东西,这将是可能的。

于 2015-04-28T23:58:17.277 回答
2

有时,与当前源代码不匹配的无效 .pyc 文件会导致此问题。

我删除了所有 .pyc 以使用此 bash 刷新所有这些

find . -name "*.pyc" -exec rm -rf {} \;

于 2017-01-13T15:40:45.990 回答
1

解决这个问题的最简单和最简单的方法是将“导入信号”命令放在您正在使用的模型文件的底部。这可确保在导入该模型的信号之前加载所有模型。您必须为要导入的每个模型执行此操作(如果您使用链接到特定模型的接收器),或者在设置中“已安装应用程序”末尾的应用程序的 models.py 中执行此操作。

仅当您处理非模型类型信号时才需要其他解决方案,其中永远不会首先导入模型。

为什么在文档中没有提到这一点是一个谜,因为我刚刚被它抓住,直到我记得你必须这样做。

模型.py

from django.db import models

class MyModel(models.Model):
    myfield1 = models.CharField()
    myfield2 = models.CharField()

import signals

然后在signals.py中:

from django.db.models.signals import pre_save # Or whatever you are using
from django.dispatch import receiver

from .models import MyModel

@receiver(pre_save, sender=MyModel)
def my_receiver(sender, instance, **kwargs):
    mysender = sender
    print mysender

像那样。

于 2016-02-29T23:23:46.590 回答
0

我在 django 1.7 中没有收到任何错误。在迁移到 django 1.8 时出现此错误。原因是信号被定义在app/signal_receiver.py.

我创建了一个apps.py

from django.apps import AppConfig

class TasksConfig(AppConfig):
    name = 'core'
    verbose_name = "core"

    def ready(self):
        import core.signal.handler

我将信号接收器移到handler.py信号包内。

于 2016-01-22T09:29:36.320 回答
-1

我收到此错误是因为有错误:

urlpatterns = patterns(
url(r'^my_link/$', views.MyView.as_view(), name='my_view'),...

做对:

urlpatterns = patterns(
'', # Add this line
url(r'^my_link/$', views.MyView.as_view(), name='my_view'),
...)
于 2021-12-09T12:11:09.653 回答