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

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')
    ...

其他回答

您是否遗漏了将应用程序名称放入设置文件中? myAppNameConfig是由.manage.py createapp myAppName命令在apps.py中生成的默认类。其中myAppName是应用程序的名称。

settings.py

INSTALLED_APPS = [
'myAppName.apps.myAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

这样,设置文件就会找到您想要调用应用程序的内容。您可以通过在apps.py文件中添加以下代码来更改它的外观

我的应用名称/应用程序.py

class myAppNameConfig(AppConfig):
    name = 'myAppName'
    verbose_name = 'A Much Better Name'

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

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

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

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

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

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

我得到同样的错误,我不知道如何解决这个问题。我花了好几个小时才发现django中的manage.py和init.py在同一个目录下。

之前:

|-- myproject
  |-- __init__.py  <---
  |-- manage.py
  |-- myproject
    |-- ...
  |-- app1
    |-- models.py
  |-- app2
    |-- models.py

后:

|-- myproject
  |-- manage.py
  |-- myproject
    |-- ...
  |-- app1
    |-- models.py
  |-- app2
    |-- models.py

当你得到这个“没有声明一个显式的app_label”错误时,你会感到非常困惑。但是删除这个init文件解决了我的问题。

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

SECRET_KEY = os.getenv('SECRET_KEY')

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