我真是一筹莫及。经过十几个小时的故障排除,可能更多,我以为我终于可以做生意了,但接着我发现:
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
网上关于这方面的信息太少了,没有解决方案可以解决我的问题。任何建议都将不胜感激。
我使用的是Python 3.4和Django 1.10。
从我的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',
]
我的app .py文件是这样的:
from django.apps import AppConfig
class DeletenoteConfig(AppConfig):
name = 'DeleteNote'
and
from django.apps import AppConfig
class LibrarysyncConfig(AppConfig):
name = 'LibrarySync'
我只是遇到了这个问题,并找出了哪里出了问题。由于之前的回答没有描述这个问题发生在我身上,我想我可以把它发布给其他人:
the issue came from using python migrate.py startapp myApp from my project root folder, then move myApp to a child folder with mv myApp myFolderWithApps/.
I wrote myApp.models and ran python migrate.py makemigrations. All went well.
then I did the same with another app that was importing models from myApp. Kaboom! I ran into this error, while performing makemigrations. That was because I had to use myFolderWithApps.myApp to reference my app, but I had forgotten to update MyApp/apps.py. So I corrected myApp/apps.py, settings/INSTALLED_APPS and my import path in my second app.
but then the error kept happening: the reason was that I had migrations trying to import the models from myApp with the wrong path. I tried to correct the migration file, but I went at the point where it was easier to reset the DB and delete the migrations to start from scratch.
长话短说:
-这个问题最初是来自myApp的apps.py中的应用程序名称错误,在设置和我的第二个应用程序的导入路径。
-但仅仅纠正这三个地方的路径是不够的,因为导入时引用了错误的应用程序名称。因此,同样的错误在迁移过程中不断发生(除了这次是从迁移中)。
所以…检查您的迁移,祝您好运!
这里没有一个答案解决了我的问题,把我们都带到这里的错误消息对我来说是一个转移注意力的错误消息——但我确实找到了一个解决方案。
对我来说,这个问题的真正原因是:
Django tries to register apps
Some exception occurs during app registration (root cause)
Something in exception handling tooling pulls in a model somewhere
That model lives in an app that hasn't been registered (because, remember, app registration was broken by the root cause exception above)
Finally, as its last act before dying Django spits out the (red herring) complaint that brought us all here - i.e. SomeModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
而不是关心这个错误(即把我们带到这里的错误),我需要向上滚动并阅读第一条错误消息。
这可能看起来像别的东西,它可以是任何打破应用注册。对我来说,根本原因是:
Traceback (most recent call last):
[...SNIP...]
File "/Users/user/.pyenv/versions/appName_py3/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/user/.pyenv/versions/appName_py3/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
[... SNIP ...]
File "/Users/user/app_name/api/models/models.py", line 1122, in <module>
class SomeObject(models.Model):
File "/Users/user/dev_alt/app_name/api/models/models.py", line 1134, in SomeObject
some_property = models.ForeignKey(SomeOtherObject, null=True, blank=True)
TypeError: __init__() missing 1 required positional argument: 'on_delete'
[...SNIP...]
During handling of the above exception, another exception occurred:
<RED HERRING STACK TRACE THAT BROUGHT US ALL HERE>
同样,“根本原因”问题对你来说可能不同——但对我来说是不同的:我正在从1.11升级一个遗留的Django应用程序。X ~ 3.2.x。在此过程中,Django做了一个破坏向后兼容性的改变,要求模型上所有的ForeignKey和OneToOne属性都有一个on_delete参数。
我为应用程序中的200多个违规情况添加了这个参数,我的根本原因问题和没有声明显式app_label问题都得到了解决。
作为一个使用Python3的新手,我发现这可能是一个导入错误而不是Django错误
错误的:
from someModule import someClass
正确的:
from .someModule import someClass
这发生在几天前,但我真的无法重现它…我认为只有刚接触Django的人才会遇到这种情况。以下是我所记得的:
尝试在admin.py中注册一个模型:
from django.contrib import admin
from user import User
admin.site.register(User)
尝试运行服务器,错误如下所示
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
将user改为.user,问题就解决了
我在测试中导入模型时遇到了这个错误,即给出这个Django项目结构:
|-- myproject
|-- manage.py
|-- myproject
|-- myapp
|-- models.py # defines model: MyModel
|-- tests
|-- test_models.py
在文件test_models.py中导入MyModel:
from models import MyModel
如果以这种方式导入,问题就解决了:
from myapp.models import MyModel
希望这能有所帮助!
PS:也许这有点晚了,但我在其他人的答案中没有发现如何解决我的代码中的这个问题,我想分享我的解决方案。