Python Django 模型“未声明显式 app_label”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40206569/
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 model "doesn't declare an explicit app_label"
提问by Slbox
I'm at wit's end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:
我已经无计可施了。经过十几个小时的故障排除,可能更多,我以为我终于开始营业了,但后来我得到了:
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
There is SO LITTLE info on this on the web, and no solution out there has resolved my issue. Any advice would be tremendously appreciated.
网上关于这方面的信息太少了,没有解决方案可以解决我的问题。任何建议将不胜感激。
I'm using Python 3.4 and Django 1.10.
我正在使用 Python 3.4 和 Django 1.10。
From my settings.py:
从我的settings.py:
INSTALLED_APPS = [
'DeleteNote.apps.DeletenoteConfig',
'LibrarySync.apps.LibrarysyncConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
And my apps.py files look like this:
我的 apps.py 文件如下所示:
from django.apps import AppConfig
class DeletenoteConfig(AppConfig):
name = 'DeleteNote'
and
和
from django.apps import AppConfig
class LibrarysyncConfig(AppConfig):
name = 'LibrarySync'
采纳答案by Xeberdee
Are you missing putting in your application name into the settings file?
The myAppNameConfig
is the default class generated at apps.py by the .manage.py createapp myAppNamecommand. Where myAppNameis the name of your app.
您是否缺少将应用程序名称放入设置文件中?这myAppNameConfig
是由.manage.py createapp myAppName命令在 apps.py 中生成的默认类。其中myAppName是您的应用程序的名称。
settings.py
设置.py
INSTALLED_APPS = [
'myAppName.apps.myAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
This way, the settings file finds out what you want to call your application. You can change how it looks later in the apps.py file by adding the following code in
这样,设置文件会找出您要调用的应用程序。您可以通过在 apps.py 文件中添加以下代码来更改它稍后在 apps.py 文件中的外观
myAppName/apps.py
myAppName/apps.py
class myAppNameConfig(AppConfig):
name = 'myAppName'
verbose_name = 'A Much Better Name'
回答by samuel
I get the same error and I don′t know how to figure out this problem. It took me many hours to notice that I have a init.py at the same direcory as the manage.py from django.
我得到同样的错误,我不知道如何解决这个问题。我花了好几个小时才注意到我有一个 init.py 与来自 django 的 manage.py 位于同一目录。
Before:
前:
|-- myproject
|-- __init__.py
|-- manage.py
|-- myproject
|-- ...
|-- app1
|-- models.py
|-- app2
|-- models.py
After:
后:
|-- myproject
|-- manage.py
|-- myproject
|-- ...
|-- app1
|-- models.py
|-- app2
|-- models.py
It is quite confused that you get this "doesn't declare an explicit app_label" error. But deleting this initfile solved my problem.
您收到此“未声明明确的 app_label”错误,这让您感到非常困惑。但是删除这个init文件解决了我的问题。
回答by stasdeep
I had exactly the same error when running tests with PyCharm. I've fixed it by explicitly setting DJANGO_SETTINGS_MODULE
environment variable. If you're using PyCharm, just hit Edit Configurationsbutton and choose Environment Variables.
使用 PyCharm 运行测试时,我遇到了完全相同的错误。我已经通过显式设置DJANGO_SETTINGS_MODULE
环境变量来修复它。如果您使用的是 PyCharm,只需点击Edit Configurations按钮并选择Environment Variables。
Set the variable to your_project_name.settings
and that should fix the thing.
将变量设置为your_project_name.settings
,应该可以解决问题。
It seems like this error occurs, because PyCharm runs tests with its own manage.py
.
似乎发生此错误,因为 PyCharm 使用自己的manage.py
.
回答by lukeaus
I got this one when I used ./manage.py shell
then I accidentally imported from the root project level directory
我在使用时得到了这个./manage.py shell
然后我不小心从根项目级别目录导入
# don't do this
from project.someapp.someModule import something_using_a_model
# do this
from someapp.someModule import something_using_a_model
something_using_a_model()
回答by Wreeecks
I had the same problem just now. I've fixed mine by adding a namespace on the app name. Hope someone find this helpful.
我刚才遇到了同样的问题。我通过在应用程序名称上添加命名空间来修复我的问题。希望有人觉得这有帮助。
apps.py
应用程序.py
from django.apps import AppConfig
class SalesClientConfig(AppConfig):
name = 'portal.sales_client'
verbose_name = 'Sales Client'
回答by rpstw
as a noob using Python3,I find it might be an import error instead of a Django error
作为使用Python3 的菜鸟,我发现这可能是导入错误而不是 Django 错误
wrong:
错误的:
from someModule import someClass
right:
对:
from .someModule import someClass
this happens a few days ago but I really can't reproduce it...I think only people new to Django may encounter this.here's what I remember:
这发生在几天前,但我真的无法重现它......我认为只有刚接触 Django 的人才会遇到这个。这是我记得的:
try to register a model in admin.py:
尝试在 admin.py 中注册一个模型:
from django.contrib import admin
from user import User
admin.site.register(User)
try to run server, error looks like this
尝试运行服务器,错误看起来像这样
some lines...
File "/path/to/admin.py" ,line 6
tell you there is an import error
some lines...
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
change user
to .user
,problem solved
变化user
来.user
,问题就解决了
回答by juliocesar
I got this error on importing models in tests, i.e. given this Django project structure:
我在测试中导入模型时遇到此错误,即鉴于此 Django 项目结构:
|-- myproject
|-- manage.py
|-- myproject
|-- myapp
|-- models.py # defines model: MyModel
|-- tests
|-- test_models.py
in file test_models.py
I imported MyModel
in this way:
在test_models.py
我MyModel
以这种方式导入的文件中:
from models import MyModel
The problem was fixed if it is imported in this way:
如果以这种方式导入,则问题已解决:
from myapp.models import MyModel
Hope this helps!
希望这可以帮助!
PS: Maybe this is a bit late, but I not found in others answers how to solve this problem in my code and I want to share my solution.
PS:也许这有点晚了,但我没有在其他人的答案中找到如何在我的代码中解决这个问题,我想分享我的解决方案。
回答by ZaMy
After keep on running into this issue and keep on coming back to this question I thought I'd share what my problem was.
在继续遇到这个问题并继续回到这个问题之后,我想我会分享我的问题是什么。
Everything that @Xeberdee is correct so follow that and see if that solves the issue, if not this was my issue:
@Xeberdee 的所有内容都是正确的,因此请遵循并查看是否解决了问题,如果不是,这是我的问题:
In my apps.py this is what I had:
在我的 apps.py 这就是我所拥有的:
class AlgoExplainedConfig(AppConfig):
name = 'algo_explained'
verbose_name = "Explain_Algo"
....
And all I did was I added the project name in front of my app name like this:
我所做的只是在我的应用程序名称前面添加了项目名称,如下所示:
class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"
and that solved my problem and I was able to run the makemigrations and migrate command after that! good luck
这解决了我的问题,之后我能够运行 makemigrations 和 migrate 命令!祝你好运
回答by inostia
I had this error today trying to run Django tests because I was using the shorthand from .models import *
syntax in one of my files. The issue was that I had a file structure like so:
我今天在尝试运行 Django 测试时遇到了这个错误,因为我from .models import *
在我的一个文件中使用了速记语法。问题是我有一个像这样的文件结构:
apps/
myapp/
models/
__init__.py
foo.py
bar.py
and in models/__init__.py
I was importing my models using the shorthand syntax:
在models/__init__.py
我使用速记语法导入我的模型时:
from .foo import *
from .bar import *
In my application I was importing models like so:
在我的应用程序中,我像这样导入模型:
from myapp.models import Foo, Bar
This caused the Django model doesn't declare an explicit app_label
when running ./manage.py test
.
这导致Django model doesn't declare an explicit app_label
运行时./manage.py test
.
To fix the problem, I had to explicitly import from the full path in models/__init__.py
:
为了解决这个问题,我必须从完整路径中显式导入models/__init__.py
:
from myapp.models.foo import *
from myapp.models.bar import *
That took care of the error.
这解决了错误。
H/t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a
H/t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a
回答by jbothma
In my case, this was happening because I used a relative module path in project-level urls.py, INSTALLED_APPS
and apps.py
instead of being rooted in the project root. i.e. absolute module paths throughout, rather than relative modules paths + hacks.
就我而言,发生这种情况是因为我在项目级urls.py 中使用了相对模块路径,INSTALLED_APPS
而apps.py
不是在项目根目录中。即始终使用绝对模块路径,而不是相对模块路径 + hacks。
No matter how much I messed with the paths in INSTALLED_APPS
and apps.py
in my app, I couldn't get both runserver
and pytest
to work til all three of those were rooted in the project root.
不管多少我与路径混乱INSTALLED_APPS
,并apps.py
在我的应用程序,我不能同时获得runserver
和pytest
工作直到所有三个那些植根于项目根。
Folder structure:
文件夹结构:
|-- manage.py
|-- config
|-- settings.py
|-- urls.py
|-- biz_portal
|-- apps
|-- portal
|-- models.py
|-- urls.py
|-- views.py
|-- apps.py
With the following, I could run manage.py runserver
and gunicorn with wsgi and use portal
app views without trouble, but pytest would error with ModuleNotFoundError: No module named 'apps'
despite DJANGO_SETTINGS_MODULE
being configured correctly.
通过以下内容,我可以manage.py runserver
使用 wsgi运行和 gunicorn 并毫无问题地使用portal
应用程序视图,但是ModuleNotFoundError: No module named 'apps'
尽管DJANGO_SETTINGS_MODULE
配置正确,pytest 还是会出错。
config/settings.py:
配置/设置.py:
INSTALLED_APPS = [
...
"apps.portal.apps.PortalConfig",
]
biz_portal/apps/portal/apps.py:
biz_portal/apps/portal/apps.py:
class PortalConfig(AppConfig):
name = 'apps.portal'
config/urls.py:
配置/urls.py:
urlpatterns = [
path('', include('apps.portal.urls')),
...
]
Changing the app reference in config/settings.py to biz_portal.apps.portal.apps.PortalConfig
and PortalConfig.name
to biz_portal.apps.portal
allowed pytest to run (I don't have tests for portal
views yet) but runserver
would error with
改变应用程序的参考配置/ settings.py来biz_portal.apps.portal.apps.PortalConfig
和PortalConfig.name
到biz_portal.apps.portal
允许pytest来运行(我没有做检查portal
的意见还),但runserver
将与错误
RuntimeError: Model class apps.portal.models.Business doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
RuntimeError:模型类 apps.portal.models.Business 未声明显式 app_label 并且不在 INSTALLED_APPS 中的应用程序中
Finally I grepped for apps.portal
to see what's still using a relative path, and found that config/urls.py should also use biz_portal.apps.portal.urls
.
最后,我apps.portal
尝试查看仍在使用相对路径的内容,发现 config/urls.py 也应该使用biz_portal.apps.portal.urls
.