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

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'

当前回答

读完另一个答案后。我能理解。根据不同的情况使用不同的方法。 我来解释一下。我是怎么破案的。这也会帮助其他人

test_model.py pycharm和模型类的pycharm映射中没有显示错误或警告

在测试运行时显示如下错误

python manage.py test cheese

解决方案

from everych.cheese.models import Dodavatel

没有任何错误的输出:

结论:大多数建议在测试类中导入应用程序模型类的答案是有问题的。检查导入是否正确

其他回答

作为一个使用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,问题就解决了

在我的例子中,在settings.py中的BASE_DIR有一个问题。这是包的结构:

project_root_directory
└── service_package
    └── db_package
        ├── my_django_package
        │       └── my_django_package
        │          ├── settings.py
        │          └── ...
        └── my_django_app
               ├── migrations
               ├── models.py
               └── ...

它在更新settings.py时工作:

INSTALLED_APPS = [
    'some_django_stuff_here...',
    'some_django_stuff_here....',
    ...
    'service_package.db_package.my_django_app'
]

BASE_DIR指向项目根目录

BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent.parent

在调试中运行django,在registry.py中使用断点-> def get_containing_app_config(self, object_name)

还要检查您的迁移是否正常工作

Python3 manage.py makemigrations
Python3 manage.py migrate

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

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

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

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

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

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