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

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'

当前回答

很可能您有依赖的导入。

在我的例子中,我在我的模型中使用了一个序列化器类作为参数,并且序列化器类使用了这个模型: serializer_class = AccountSerializer

from ..api.serializers import AccountSerializer

class Account(AbstractBaseUser):
    serializer_class = AccountSerializer
    ...

在“serializers”文件中:

from ..models import Account

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = (
            'id', 'email', 'date_created', 'date_modified',
            'firstname', 'lastname', 'password', 'confirm_password')
    ...

其他回答

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

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

我在Django rest_framework中构建API时遇到了类似的错误。

模型类apps.core.models.University没有显式声明> app_label,也不在INSTALLED_APPS中的应用程序中。

Luke_aus的回答纠正了我的urls.py

from

from project.apps.views import SurgeryView

to

from apps.views import SurgeryView

在我的例子中,我在将代码从Django 1.11.11移植到Django 2.2时得到了这个错误。我正在定义一个自定义的FileSystemStorage派生类。在Django 1.11.11中,我在models.py中有如下一行:

from django.core.files.storage import Storage, DefaultStorage

然后在文件中我有类定义:

class MyFileStorage(FileSystemStorage):

然而,在Django 2.2中,我需要在导入时显式引用FileSystemStorage类:

from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage

瞧!,错误消失。

注意,每个人都在报告Django服务器吐出的错误消息的最后一部分。然而,如果你向上滚动,你会在错误的中间找到原因。

我在测试中导入模型时遇到了这个错误,即给出这个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:也许这有点晚了,但我在其他人的答案中没有发现如何解决我的代码中的这个问题,我想分享我的解决方案。

我在使用。/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()