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

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

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

其他回答

O G M… 我也得到了这个错误,我花了将近2天的时间,现在我终于设法解决了它。老实说……这个错误与问题本身无关。 对我来说,这只是一个简单的语法问题。我试图运行一个独立的python模块,在django上下文中使用一些django模型,但模块本身不是django模型。但我认为这门课是错的

而不是

class Scrapper:
    name = ""
    main_link= ""
    ...

我在做

class Scrapper(Website):
    name = ""
    main_link= ""
    ...

这显然是错误的。这个信息是如此的误导,以至于我忍不住认为这是配置上的一些问题,或者只是使用django的方式错误,因为我对它很陌生。

我将在这里分享给那些新手,因为我经历了同样的愚蠢,希望能解决他们的问题。

您是否遗漏了将应用程序名称放入设置文件中? 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'

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

manage.py makemigrations myapp

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

manage.py makemigrations

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

在我的例子中,我在manage.py shell中尝试了相同的导入。shell被更新的db搞砸了。我的意思是,我忘记在更新DB后重新启动我的shell。

为了解决这个问题,我必须通过以下命令停止shell:

>>> exit()

然后执行如下命令重新启动shell:

$ python3 manage.py shell

希望这能帮助到像我这样的人。

我只是遇到了这个问题,并找出了哪里出了问题。由于之前的回答没有描述这个问题发生在我身上,我想我可以把它发布给其他人:

the issue came from using python migrate.py startapp myApp from my project root folder, then move myApp to a child folder with mv myApp myFolderWithApps/. I wrote myApp.models and ran python migrate.py makemigrations. All went well. then I did the same with another app that was importing models from myApp. Kaboom! I ran into this error, while performing makemigrations. That was because I had to use myFolderWithApps.myApp to reference my app, but I had forgotten to update MyApp/apps.py. So I corrected myApp/apps.py, settings/INSTALLED_APPS and my import path in my second app. but then the error kept happening: the reason was that I had migrations trying to import the models from myApp with the wrong path. I tried to correct the migration file, but I went at the point where it was easier to reset the DB and delete the migrations to start from scratch.

长话短说: -这个问题最初是来自myApp的apps.py中的应用程序名称错误,在设置和我的第二个应用程序的导入路径。 -但仅仅纠正这三个地方的路径是不够的,因为导入时引用了错误的应用程序名称。因此,同样的错误在迁移过程中不断发生(除了这次是从迁移中)。

所以…检查您的迁移,祝您好运!