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

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中的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文件解决了我的问题。

其他回答

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

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中的应用程序名称错误,在设置和我的第二个应用程序的导入路径。 -但仅仅纠正这三个地方的路径是不够的,因为导入时引用了错误的应用程序名称。因此,同样的错误在迁移过程中不断发生(除了这次是从迁移中)。

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

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

manage.py makemigrations myapp

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

manage.py makemigrations

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

如果所有配置都是正确的,导入时可能会一团糟。注意您是如何导入有问题的模型的。

下面的代码在.models import Business中不起作用。请使用完全导入路径:from myapp。模型进口业务

我在测试中导入模型时遇到了这个错误,即给出这个Django项目结构:

|-- myproject
    |-- manage.py
    |-- myproject
    |-- myapp
        |-- models.py  # defines model: MyModel
        |-- tests
            |-- test_models.py

在文件test_models.py中导入MyModel:

from models import MyModel

如果以这种方式导入,问题就解决了:

from myapp.models import MyModel

希望这能有所帮助!

PS:也许这有点晚了,但我在其他人的答案中没有发现如何解决我的代码中的这个问题,我想分享我的解决方案。

很可能您有依赖的导入。

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