Django 1.9弃用警告app_label

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

/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): 

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

  1. 根据文档 , Options.app_label不是Options.app_label除非模型在应用程序模块之外,在我的情况下,它不是。 其次,这个行为在1.7中被弃用了,为什么这个问题呢?
  2. 应用程序都在INSTALLED_APPS元组中,所以它肯定不能这样?
  3. 如果一切都在INSTALLED_APPS元组中,为什么在被调用之前应用程序不会被加载?

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

12 Solutions collect form web for “Django 1.9弃用警告app_label”

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

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

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

通常情况下,当你from .models import SomeModels apps.py早期信号中的SomeModels (例如post_migrate )时,会发生这种情况。 这里推荐使用AppConfig.get_model() ,而不是用经典的方式引用你的模型。 检查你的apps.py文件中的任何模型导入,并用这个APIreplace它们。

例如,而不是:

 # 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) 

注意这个实施是在bug #21719中引入的。

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

 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): 

我的解决scheme是:

'django.contrib.sites'添加到INSTALLED_APPS

我怀疑只有极less数的人会看到这个错误,这个错误是由我和我一样的东西造成的,但是如果它帮助别人,似乎值得添加这个答案!

在一次运行testing时,我突然看到了很多这样的错误 – 事实certificate,我意外地在一个子目录中时,意外地在我的Django项目的顶层创build了一个__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 

我得到了一个类似的错误,而不是抱怨在我的应用程序模型,它抱怨在contrib包中的模型。 例如:

C:\ Program Files \ Python 2.7 \ lib \ site-packages \ django \ contrib \ sessions \ models.py:27:RemovedInDjango19Warning:Model class django.contrib.sessions.models.Session does not declare a explicit app_label and isn在INSTALLED_APPS中的应用程序中,或者在其应用程序被加载之前被导入。 这将不再支持在Django 1.9中。 class Session(models.Model):

这是由settings.py INSTALLED_APPS属性的错误sorting造成的。 我的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粘贴错误时唯一显示的链接,所以我在这里回答了。

但是 ,我相信类似的情况正在导致你的问题:

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

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

 class Meta: app_label = 'app_model_belongs_to' 

希望这个作品!

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

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

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

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

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

希望这可以帮助别人!

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

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

基本上,如果这个时候你的应用程序不存在,你会得到这个警告。 我意识到我的问题是我有一个第三方框架(在我的情况下,干草堆),试图导入我的自定义模型之一。

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

处理这个Django 1.9的方式,并给你的应用程序在pipe理员一个不错的名字是做到以下几点:

在您的名为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' 

升级后的Django从1.8到1.9.1,我有这个问题:

RuntimeError在/

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

这有助于解决:

在blog / models.py中:

 class BlogCategory(models.Model): some vars & methods class Meta: app_label = 'BlogCategory' 

这是100%的工作。

有时一个无效的.pyc文件与当前源代码不匹配导致了这个问题。

我删除了所有的.pyc使用这个bash刷新所有的

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

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

其他解决scheme只有在处理非模型types信号时才需要,其中模型将永远不会先导入。

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

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 1.7中没有收到任何错误。 当迁移到Django的1.8我得到这个错误。 原因是信号是在app/signal_receiver.py中定义的。

我创build了一个apps.py

 from django.apps import AppConfig class TasksConfig(AppConfig): name = 'core' verbose_name = "core" def ready(self): import core.signal.handler 

信号包里面的handler.py我移动了信号接收器。

  • 如何发送一个“多部分/forms的数据”在Python中的请求?
  • 从Python中的string中剥离HTML
  • 正则expression式匹配跨平台换行符
  • 强制Python放弃原生sqlite3并使用(安装的)最新的sqlite3版本
  • Python的json.loads显示ValueError:额外的数据
  • Jython什么时候会支持Python 3?
  • Eratosthenes的筛子 - find首席Python
  • 如何在matplotlib / Python中切换后端
  • Pythonunit testing:以编程方式生成多个testing?
  • 如何在Python中连接到MySQL数据库?
  • 打包和发送python库和脚本,专业的方式