Python Django 1.9 弃用警告 app_label
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29635765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django 1.9 deprecation warnings app_label
提问by Neil Hickman
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.
我刚刚更新到 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):
Now this poses 3 questions for me.
现在这对我提出了 3 个问题。
- 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? - The applications are all in the INSTALLED_APPS tuple, so it surely can't be that?
- Why would the applications not be loaded before they are called if everything is in the INSTALLED_APPS tuple?
- 根据文档,
Options.app_label
除非模型在应用程序模块之外,否则不是必需的,在我的情况下,它不是。其次,无论如何,这种行为在 1.7 中已被弃用,那么为什么它甚至是一个问题? - 应用程序都在 INSTALLED_APPS 元组中,所以它肯定不会是这样吗?
- 如果所有内容都在 INSTALLED_APPS 元组中,为什么在调用应用程序之前不会加载它们?
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.
如果我确实做错了什么,那么正确的做法是什么,因为文档并没有真正弄清楚是什么导致了这个问题或如何纠正它。
采纳答案by Antwan
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.
- 当您使用不在
INSTALLED_APPS
; 中的模型时 - 或者当您在加载应用程序之前使用模型时。
Since you did refer the app in the INSTALLED_APPS
setting, this is most likely you're using a model before the app initialisation.
由于您确实在INSTALLED_APPS
设置中引用了应用程序,因此很可能您在应用程序初始化之前使用了模型。
Typically, this occurs when you have from .models import SomeModels
in an apps.pyearly 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.pyfile for any model import, and replace them using this api.
通常,当您from .models import SomeModels
在apps.py 中有早期信号(例如post_migrate
)时会发生这种情况。建议使用AppConfig.get_model(),而不是在这里以经典方式引用您的模型。检查您的apps.py文件是否有任何模型导入,并使用此 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.
请注意,此强制措施是在错误#21719 中引入的。
回答by Justin Ober
Add a meta class to your model with an app_label attribute.
使用 app_label 属性向模型添加元类。
class Meta:
app_label = 'app_model_belongs_to'
Hope this works!
希望这有效!
EDIT:The reason for this is usually that the model exists outside of the standard locations.
编辑:这样做的原因通常是模型存在于标准位置之外。
For more information refer to: https://docs.djangoproject.com/en/1.8/ref/models/options/#app-label
更多信息请参考:https: //docs.djangoproject.com/en/1.8/ref/models/options/#app-label
回答by Anthony Choi
I had the same problem. I put a breakpoint in django.db.models.base.py:line82 and try to figure out what is causing this warning message.
我有同样的问题。我在 django.db.models.base.py:line82 中放置了一个断点,并尝试找出导致此警告消息的原因。
# Look for an application configuration to attach the model to.
app_config = apps.get_containing_app_config(module)
Basically, if your app does not exist by this time, you get that warning. I realized that my problem was that I have a third party framework (in my case, haystack) that tries to import one of my custom models.
基本上,如果此时您的应用程序不存在,您会收到该警告。我意识到我的问题是我有一个第三方框架(在我的例子中是 haystack)试图导入我的自定义模型之一。
Maybe you also have a third party package listed in INSTALLED_APPS before your custom apps and your third party package references your custom apps? This would be possible if you are using something like Django rest framework as well.
也许在您的自定义应用程序和您的第三方程序包引用您的自定义应用程序之前,您在 INSTALLED_APPS 中还列出了第三方程序包?如果您也使用 Django rest 框架之类的东西,这也是可能的。
回答by Vlad Schnakovszki
I was getting a similar error, but rather than complaining about models in my apps, it was complaining about models in contrib
packages. For example:
我遇到了类似的错误,但不是抱怨我的应用程序中的模型,而是抱怨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):
C:\Program Files\Python 2.7\lib\site-packages\django\contrib\sessions\models.py:27: RemovedInDjango19Warning: 模型类 django.contrib.sessions.models.Session 没有声明一个明确的 app_label 并且不是'不是在 INSTALLED_APPS 中的应用程序中,或者在其应用程序加载之前导入。这将在 Django 1.9 中不再受支持。类会话(模型。模型):
This is caused by a bad ordering in the INSTALLED_APPS
property in settings.py
. My settings.py
initially contained:
这是由 中的INSTALLED_APPS
属性中的错误排序引起的settings.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_*
use models from the contrib
packages. The error is caused by using models before declaring them (i.e. Django
should know about the apps containing those models beforeusing them).
my_app_*
使用contrib
包中的模型。该错误是通过声明他们之前使用的模型引起的(即Django
应该知道包含这些模型的应用程序之前使用它们)。
In order to solve this, the order in which the apps are declared needs to be changed. Specifically, all Django apps should come before the user defined apps. In my case, the correct INSTALLED_APPS
would look like:
为了解决这个问题,需要更改应用程序声明的顺序。具体来说,所有 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',
)
Now I know this might not directly answer your question, but it answers a related one, and since this is the only SO link that shows on Google when pasting the error, I have answered it here.
现在我知道这可能不会直接回答你的问题,但它回答了一个相关的问题,因为这是粘贴错误时在谷歌上显示的唯一 SO 链接,我在这里回答了它。
However, I believe a similar situation is causing your problem:
但是,我相信类似的情况会导致您的问题:
Make sure you declare the "dependency" apps before the apps using them!The errors don't really specify which app is using a model, so you would have to push the app containing the model it mentions to the top, one by one, until the error disappears.
确保在使用它们的应用程序之前声明“依赖”应用程序!错误并没有真正指定哪个应用程序正在使用模型,因此您必须将包含它提到的模型的应用程序一个一个地推到顶部,直到错误消失。
回答by WayBehind
Similar error. In my case the error was:
类似的错误。在我的情况下,错误是:
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):
My solution was:
我的解决方案是:
Added 'django.contrib.sites'
to INSTALLED_APPS
添加'django.contrib.sites'
到INSTALLED_APPS
回答by NathanQ
The Django 1.9 way of handling this and giving your app a nice name in the admin is to do the following:
Django 1.9 处理此问题并在管理员中为您的应用程序命名的方法是执行以下操作:
Add a file in your app named apps.py
and add the following to it:
在您的应用程序中添加一个名为的文件apps.py
并向其中添加以下内容:
#apps.py
from django.apps import AppConfig
class YourAppNameAppConfig(AppConfig):
name = 'yourappname'
verbose_name = 'Your App Name Looking Right'
Then, in your app's __init__.py
file, add the following:
然后,在您的应用程序__init__.py
文件中,添加以下内容:
#__init__.py
default_app_config = 'youappname.apps.YourAppNameAppConfig'
回答by Caumons
I faced this issue as well and it was related to the way I was loading the signals.py modulefrom an app.
我也遇到了这个问题,它与我从应用程序加载signals.py 模块的方式有关。
As you may now, it's quite common to have circular import issues between the signals and the models, and it's usual to import them from the app's __init__.py
file or from the bottom of the models.py
file to avoid them.
正如您现在可能看到的,信号和模型之间存在循环导入问题是很常见的,通常从应用程序的__init__.py
文件或文件底部导入它们models.py
以避免它们。
Well, I was using the __init__.py
approach, and just moving the import signals
statement to the bottom of my models.py
file solved the issue.
好吧,我正在使用该__init__.py
方法,只需将import signals
语句移动到models.py
文件底部即可解决问题。
Hope this helps someone else!
希望这对其他人有帮助!
回答by Роман Арсеньев
I has this problem after upgrade Django from 1.8 to 1.9.1:
将 Django 从 1.8 升级到 1.9.1 后,我遇到了这个问题:
RuntimeError at /
运行时错误在 /
Model class blog.models.BlogCategory doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
模型类 blog.models.BlogCategory 未声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中。
This help to resolve:
这有助于解决:
in blog/models.py:
在博客/models.py 中:
class BlogCategory(models.Model):
some vars & methods
class Meta:
app_label = 'BlogCategory'
It's work on 100%.
这是 100% 的工作。
回答by Netro
I didn't receive any error in django 1.7.
While migrating to django 1.8 I got this error.
Reason was the signal were defined in app/signal_receiver.py
.
我在 django 1.7 中没有收到任何错误。在迁移到 django 1.8 时,我收到了这个错误。原因是信号在app/signal_receiver.py
.
I created a apps.py
我创建了一个 apps.py
from django.apps import AppConfig
class TasksConfig(AppConfig):
name = 'core'
verbose_name = "core"
def ready(self):
import core.signal.handler
I moved signal receiver in handler.py
inside signal package.
我在handler.py
内部信号包中移动了信号接收器。
回答by MontyThreeCard
The easiest and simplest way to solve this is to place the "import signals" command at the BOTTOM of the Model file you are working with. This ensures that the models are all loaded before the signals for that model are imported. You would have to do this for each model you are importing (if you use receivers linked to particular models), or in the models.py for the app that comes at the end of the "installed apps" in your settings.
解决这个问题的最简单和最简单的方法是将“导入信号”命令放在您正在使用的模型文件的底部。这可确保在导入该模型的信号之前加载所有模型。您必须为要导入的每个模型(如果您使用链接到特定模型的接收器)或在设置中“已安装的应用程序”末尾的应用程序的 models.py 中执行此操作。
The other solutions are only required if you are dealing with non-model type signals, where the models will never be imported first.
仅当您处理非模型类型信号时才需要其他解决方案,其中模型永远不会首先导入。
Why this is not noted in the docs is a mystery, because I just got caught by it, until I remembered that you have to do that.
为什么在文档中没有提到这一点是个谜,因为我刚刚被它吸引住了,直到我想起你必须这样做。
models.py
模型.py
from django.db import models
class MyModel(models.Model):
myfield1 = models.CharField()
myfield2 = models.CharField()
import signals
And then in signals.py:
然后在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
Like that.
像那样。