Django 1.9弃用警告app_label

时间:2015-04-14 19:24:02

标签: python django deprecation-warning

我刚刚更新到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元组中,为什么在调用应用程序之前不会加载它们?
  4. 如果我确实做错了什么,这样做的正确方法是什么,因为文档并没有真正弄清楚导致这个问题的原因或如何纠正它。

12 个答案:

答案 0 :(得分:50)

如警告中所述,这种情况发生在:

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

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

通常,如果您在 apps.py 早期信号中有from .models import SomeModels(例如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中引入的。

答案 1 :(得分:50)

类似的错误。在我的情况下,错误是:

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

答案 2 :(得分:23)

我怀疑只有极少数人会看到这个错误,因为它会和我一样,但是如果它对其他人有帮助,似乎值得添加这个答案!

我在一次运行测试时突然发现了很多这些错误 - 结果发现我在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

答案 3 :(得分: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将不再支持此功能。     class Session(models.Model):

这是由INSTALLED_APPSsettings.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链接,我在这里已经回答了。

然而,我认为类似的情况会导致您的问题:

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

答案 4 :(得分:8)

使用app_label属性为您的模型添加元类。

class Meta:
    app_label = 'app_model_belongs_to'

希望这有效!

修改 原因通常是模型存在于标准位置之外。

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

答案 5 :(得分:5)

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

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

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

希望这有助于其他人!

答案 6 :(得分:5)

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

RuntimeError at /

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

这有助于解决:

在blog / models.py中:

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

    class Meta:
        app_label = 'BlogCategory'

它的工作是100%。

答案 7 :(得分: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框架这样的东西,这是可能的。

答案 8 :(得分:4)

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'

答案 9 :(得分:2)

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

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

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

答案 10 :(得分:1)

解决此问题的最简单和最简单的方法是将“import signals”命令放在您正在使用的Model文件的BOTTOM上。这可确保在导入该模型的信号之前加载模型。您必须为要导入的每个模型执行此操作(如果使用链接到特定模型的接收器),或者在设置中“已安装应用程序”末尾的应用程序的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

就像那样。

答案 11 :(得分:0)

我在django 1.7中没有收到任何错误。 在迁移到django 1.8时,我遇到了这个错误。 原因是信号是在AppBundle\Entity\User: type: entity table: null repositoryClass: AppBundle\Repository\UserRepository id: id: type: integer id: true generator: strategy: AUTO fields: fbId: type: bigint options: unsigned: true firstName: type: string length: 255 中定义的。

我创建了一个apps.py

app/signal_receiver.py

我在from django.apps import AppConfig class TasksConfig(AppConfig): name = 'core' verbose_name = "core" def ready(self): import core.signal.handler 信号包内移动了信号接收器。