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

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'

当前回答

在不断遇到这个问题并不断回到这个问题之后,我想我应该分享我的问题是什么。

@Xeberdee的所有内容都是正确的,所以遵循它,看看是否解决了问题,如果没有,这是我的问题:

在我的app .py中是这样的:

class AlgoExplainedConfig(AppConfig):
    name = 'algo_explained'
    verbose_name = "Explain_Algo"
    ....

我所做的就是把项目名称加在我的应用名称前面,像这样:

class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"

这解决了我的问题,我能够运行makemigrations和migrate命令之后!祝你好运

其他回答

如果所有这些都失败了,并且当你试图在PyCharm“Python控制台”(或“Django控制台”)中导入时看到这个错误:

尝试重新启动控制台。

这很尴尬,但我过了一会儿才意识到我忘了这么做。

事情是这样的:

添加了一个新的应用程序,然后添加了一个最小模型,然后尝试在Python/Django控制台导入模型(PyCharm pro 2019.2)。这引发了“不声明app_label”的显式错误,因为我还没有将新应用添加到INSTALLED_APPS中。 所以,我将应用程序添加到INSTALLED_APPS,再次尝试导入,但仍然得到相同的错误。

来到这里,看了所有其他的答案,但似乎都不合适。

最后,我突然意识到,在将新应用程序添加到INSTALLED_APPS后,我还没有重新启动Python控制台。

注意:在向模块中添加新对象后,无法重新启动PyCharm Python控制台,也会导致非常混乱的ImportError: Cannot import name…

在不断遇到这个问题并不断回到这个问题之后,我想我应该分享我的问题是什么。

@Xeberdee的所有内容都是正确的,所以遵循它,看看是否解决了问题,如果没有,这是我的问题:

在我的app .py中是这样的:

class AlgoExplainedConfig(AppConfig):
    name = 'algo_explained'
    verbose_name = "Explain_Algo"
    ....

我所做的就是把项目名称加在我的应用名称前面,像这样:

class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"

这解决了我的问题,我能够运行makemigrations和migrate命令之后!祝你好运

很可能您有依赖的导入。

在我的例子中,我在我的模型中使用了一个序列化器类作为参数,并且序列化器类使用了这个模型: 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')
    ...

作为一个使用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测试时出现了这个错误,因为我在其中一个文件中使用了.models import *语法的简写。问题是我的文件结构是这样的:

    apps/
      myapp/
        models/
          __init__.py
          foo.py
          bar.py

在models/__init__.py中,我使用简写语法导入我的模型:

    from .foo import *
    from .bar import *

在我的应用程序中,我像这样导入模型:

    from myapp.models import Foo, Bar

这导致Django模型在运行./manage.py测试时没有显式声明app_label。

为了解决这个问题,我必须显式地从models/__init__.py中的完整路径导入:

    from myapp.models.foo import *
    from myapp.models.bar import *

这就解决了错误。

H - t https://medium.com/@michal。博克fix-weird-exceptions-when-running-django-tests-f58def71b59a