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

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'

当前回答

在我移动SECRET_KEY以从环境变量中提取,并且在运行应用程序时忘记设置它之后,我收到了这个错误。如果你的settings。py中有这样的东西

SECRET_KEY = os.getenv('SECRET_KEY')

然后确保您实际上设置了环境变量。

其他回答

很可能您有依赖的导入。

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

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

尝试重新启动控制台。

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

事情是这样的:

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

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

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

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

当我尝试为一个由于git合并而存在畸形迁移的应用程序生成迁移时,我遇到了这个错误。如。

manage.py makemigrations myapp

当我删除它的迁移,然后运行:

manage.py makemigrations

错误没有发生,迁移成功生成。

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

@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命令之后!祝你好运

这里没有一个答案解决了我的问题,把我们都带到这里的错误消息对我来说是一个转移注意力的错误消息——但我确实找到了一个解决方案。

对我来说,这个问题的真正原因是:

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问题都得到了解决。