我真是一筹莫及。经过十几个小时的故障排除,可能更多,我以为我终于可以做生意了,但接着我发现:

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'

当前回答

在使用PyCharm运行测试时,我遇到了完全相同的错误。我已经通过显式设置DJANGO_SETTINGS_MODULE环境变量来修复它。如果您正在使用PyCharm,只需点击编辑配置按钮并选择环境变量。

将变量设置为your_project_name。设置和那应该解决问题。

似乎出现了这个错误,因为PyCharm使用自己的manage.py运行测试。

其他回答

我今天得到了这个错误,在谷歌后结束了这里。现有的答案似乎都与我的情况无关。我唯一需要做的就是从应用程序顶层的__init__.py文件中导入一个模型。我必须将我的导入移动到使用模型的函数中。

Django似乎有一些奇怪的代码可以在很多不同的场景中失败!

当我试图将Django Rest Framework应用程序升级到DRF 3.6.3和Django 1.11.1时,我得到了这个错误。

对于在这种情况下的其他人,我在GitHub问题中找到了我的解决方案,这是在DRF设置中取消UNAUTHENTICATED_USER设置:

# webapp/settings.py
...
REST_FRAMEWORK = {
    ...
    'UNAUTHENTICATED_USER': None
    ...
}

TL;DR:添加一个空白__init__.py为我解决了这个问题。

我在PyCharm中得到了这个错误,并意识到我的设置文件根本没有被导入。没有明显的错误告诉我这一点,但是当我在settings.py中放入一些无意义的代码时,它并没有引起错误。

我在local_settings文件夹中有settings.py。但是,我忘记在同一个文件夹中包含__init__.py,以便导入它。一旦我添加了这个,错误就消失了。

我刚才也遇到了同样的问题。我已经通过在应用程序名称上添加一个名称空间来修复我的。希望有人觉得这有帮助。

apps.py

from django.apps import AppConfig    

class SalesClientConfig(AppConfig):
        name = 'portal.sales_client'
        verbose_name = 'Sales Client'

我只是遇到了这个问题,并找出了哪里出了问题。由于之前的回答没有描述这个问题发生在我身上,我想我可以把它发布给其他人:

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中的应用程序名称错误,在设置和我的第二个应用程序的导入路径。 -但仅仅纠正这三个地方的路径是不够的,因为导入时引用了错误的应用程序名称。因此,同样的错误在迁移过程中不断发生(除了这次是从迁移中)。

所以…检查您的迁移,祝您好运!