Django 1.9 deprecation warning app_label [英] Django 1.9 deprecation warnings app_label

查看:182
本文介绍了Django 1.9 deprecation warning app_label的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  / Users / neilhickman / Sites / guild / ankylosguild / apps / raiding / models.py:6:RemovedInDjango19警告:模型类ankylosguild.apps.raiding.models.Difficulty不声明一个显式的app_label,也不在INSTALLED_APPS中的应用程序中,否则在应用程序加载之前导入。 Django 1.9将不再支持此功能。 
class难度(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:21:RemovedInDjango19警告:模型类ankylosguild.apps.raiding .models.Zone没有声明一个明确的app_label,也不在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class Zone(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:49:RemovedInDjango19警告:模型类ankylosguild.apps.raiding .models.Boss不声明一个明确的app_label,也不会在INSTALLED_APPS中的应用程序中,否则在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class Boss(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:79:RemovedInDjango19警告:模型类ankylosguild.apps.raiding .models.Item不声明一个显式的app_label,并且不在INSTALLED_APPS中的应用程序中,否则在应用程序加载之前导入。 Django 1.9将不再支持此功能。
类项目(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:14:RemovedInDjango19警告:模型类ankylosguild.apps.forum .models.Category不声明一个显式的app_label,也不会在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class类别(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:36:RemovedInDjango19警告:模型类ankylosguild.apps.forum .models.Comment没有声明一个显式的app_label,也不是在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class评论(ScoreMixin,ProfileMixin,models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:64:RemovedInDjango19警告:模型类ankylosguild .apps.forum.models.Forum没有声明一个明确的app_label,也不在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class论坛(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:88:RemovedInDjango19警告:模型类ankylosguild.apps.forum .models.Post没有声明一个明确的app_label,也不在INSTALLED_APPS中的应用程序中,也不会在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class Post(ScoreMixin,ProfileMixin,models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:119:RemovedInDjango19警告:模型类ankylosguild .apps.forum.models.CommentPoint没有声明一个显式的app_label,并且不在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class CommentPoint(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:127:RemovedInDjango19警告:模型类ankylosguild.apps.forum .models.TopicPoint没有声明一个明确的app_label,也不会在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class TopicPoint(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/auctionhouse/models.py:10:RemovedInDjango19警告:模型类ankylosguild.apps.auctionhouse .models.Auction不声明一个明确的app_label,也不会在INSTALLED_APPS中的应用程序中,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class Auction(models.Model):

/Users/neilhickman/Sites/guild/ankylosguild/apps/auctionhouse/models.py:83:RemovedInDjango19警告:模型类ankylosguild.apps.auctionhouse .models.Bid没有声明一个明确的app_label,也没有在INSTALLED_APPS中的应用程序中声明,或者在应用程序加载之前导入。 Django 1.9将不再支持此功能。
class出价(models.Model):

现在这给我提出了3个问题。 / p>


  1. 根据文档 Options.app_label 不是一个要求,除非模型在应用程序模块之外,我的情况,不是。第二,这个行为在1.7中已经被弃用了,所以为什么还有一个问题?

  2. 应用程序都在INSTALLED_APPS元组中,所以肯定不会这样吗? >
  3. 如果一切都在INSTALLED_APPS元组中,为什么这些应用程序在被调用之前不会被加载?

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

解决方案

如警告中所述,这可能发生在:




  • 当你使用模型不在 INSTALLED_APPS ;

  • 或在加载应用程序之前使用模型时。



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



通常情况下会发生您有一个 apps.py 早期信号中的.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)
pre>

执行此操作:

 #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 中引入错误。


I've just updated to Django v1.8, and testing my local setup before updating my project and I've had a deprecation warning that I've never seen before, nor does it make any sense to me. I may be just overlooking something or misunderstanding the documentation.

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

Now this poses 3 questions for me.

  1. According to the documentation, Options.app_label isn't a requirement unless the model is outside of the application module, which in my case, it isn't. Secondly, this behaviour was deprecated in 1.7 anyway, so why is it even an issue?
  2. The applications are all in the INSTALLED_APPS tuple, so it surely can't be that?
  3. Why would the applications not be loaded before they are called if everything is in the INSTALLED_APPS tuple?

If I am indeed doing something wrong, what is the correct way of doing it as the docs don't really clear up what is causing this problem or how to rectify it.

解决方案

As stated in the warning, this happens either :

  • When you're using a model which is not in an INSTALLED_APPS;
  • Or when you're using a model before its application is loaded.

Since you did refer the app in the INSTALLED_APPS setting, this is most likely you're using a model before the app initialisation.

Typically, this occurs when you have from .models import SomeModels in an apps.py early signal (for example post_migrate). Instead of referring your models the classic way here, it is recommended to use AppConfig.get_model(). Check your apps.py file for any model import, and replace them using this api.

For example instead of :

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

Do this :

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

Note this enforcement was introduced in bug #21719.

这篇关于Django 1.9 deprecation warning app_label的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆