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

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'

当前回答

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

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

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

其他回答

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

尝试重新启动控制台。

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

事情是这样的:

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

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

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

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

我刚才也遇到了同样的问题。我已经通过在应用程序名称上添加一个名称空间来修复我的。希望有人觉得这有帮助。

apps.py

from django.apps import AppConfig    

class SalesClientConfig(AppConfig):
        name = 'portal.sales_client'
        verbose_name = 'Sales Client'

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

我有一个类似的问题,但我能够解决我的通过明确指定app_label使用元类在我的模型类

class Meta:
    app_label  = 'name_of_my_app'

在我的情况下,我能够找到一个修复程序,通过查看其他人的代码,它可能是相同的问题。我只需要添加'django.contrib。在settings.py文件中将Sites '添加到已安装应用的列表中。

希望这能帮助到一些人。这是我对编码社区的第一个贡献