繁体   English   中英

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

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

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

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

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

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

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

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项目的顶层意外创建了一个__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没有声明一个明确的app_label和isn在INSTALLED_APPS中的应用程序中,或者在加载应用程序之前导入。 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',
)

现在我知道这可能不会直接回答你的问题,但它回答了一个相关问题,因为这是粘贴错误时谷歌上显示的唯一SO链接,我在这里已经回答了。

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

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

使用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从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%的工作。

我有同样的问题。 我在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框架这样的东西,这是可能的。

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'

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

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

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

解决此问题的最简单和最简单的方法是将“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

像那样。

我在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移动了信号接收器。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM