Django 1.9 弃用警告 app_label

Django 1.9 deprecation warnings app_label

我刚刚更新到 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. 根据 documentationOptions.app_label 不是必需的,除非模型在应用程序模块之外,而在我的例子中,它不是。其次,这个行为在 1.7 中已经被弃用了,那么为什么它甚至是一个问题?
  2. 应用程序都在 INSTALLED_APPS 元组中,所以它肯定不是那个?
  3. 如果所有内容都在 INSTALLED_APPS 元组中,为什么应用程序在调用之前不会加载?

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

使用 app_label 属性向您的模型添加元 class。

class Meta:
    app_label = 'app_model_belongs_to'

希望这有效!

编辑: 出现这种情况的原因通常是模型存在于标准位置之外。

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

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

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

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

通常,当您在 apps.py 早期信号(例如 post_migrate)中有 from .models import SomeModels 时,就会发生这种情况。 建议使用 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 中引入的。

我遇到了同样的问题。我在 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 休息框架之类的东西,这将是可能的。

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

C:\Program Files\Python 2.7\lib\site-packages\django\contrib\sessions\models.py:27: RemovedInDjango19Warning: Model class django.contrib.sessions.models.Session 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 Session(models.Model):

这是由于 settings.py 中的 INSTALLED_APPS 属性 中的错误排序造成的。我的 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 link,我有在这里回答。

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

确保在应用程序使用它们之前声明 "dependency" 应用程序! 错误并没有真正指定哪个应用程序正在使用模型,所以你会将包含它提到的模型的应用程序一一推送到顶部,直到错误消失。

类似的错误。在我的例子中,错误是:

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

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'

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

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

好吧,我使用的是 __init__.py 方法,只是 import signals 语句移动到我的 models.py 文件的底部就解决了这个问题.

希望这对其他人有帮助!

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

RuntimeError 在 /

模型 class blog.models.BlogCategory 没有显式声明 app_label 并且不在 INSTALLED_APPS.[=25= 的应用程序中]

这有助于解决:

在blog/models.py:

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

    class Meta:
        app_label = 'BlogCategory'

它正在 100% 工作。

我在 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 中。

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

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

为什么文档中没有注明这一点是个谜,因为我刚刚被它抓住了,直到我想起你必须这样做。

models.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

就像那样。

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

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

/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

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

我删除了所有 .pyc 以使用此 bash

刷新所有文件

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

我收到这个错误是因为错误:

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'),
...)