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

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'

当前回答

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

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

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

其他回答

对于PyCharm用户:我有一个错误使用不“干净”的项目结构。

Was:

project_root_directory
└── src
    ├── chat
    │   ├── migrations
    │   └── templates
    ├── django_channels
    └── templates

Now:

project_root_directory
├── chat
│   ├── migrations
│   └── templates
│       └── chat
├── django_channels
└── templates

这里有很多好的解决方案,但我认为,首先,你应该清理你的项目结构或调优PyCharm Django设置之前,设置DJANGO_SETTINGS_MODULE变量等等。

希望它能帮助到别人。欢呼。

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

我今天试图运行Django测试时出现了这个错误,因为我在其中一个文件中使用了.models import *语法的简写。问题是我的文件结构是这样的:

    apps/
      myapp/
        models/
          __init__.py
          foo.py
          bar.py

在models/__init__.py中,我使用简写语法导入我的模型:

    from .foo import *
    from .bar import *

在我的应用程序中,我像这样导入模型:

    from myapp.models import Foo, Bar

这导致Django模型在运行./manage.py测试时没有显式声明app_label。

为了解决这个问题,我必须显式地从models/__init__.py中的完整路径导入:

    from myapp.models.foo import *
    from myapp.models.bar import *

这就解决了错误。

H - t https://medium.com/@michal。博克fix-weird-exceptions-when-running-django-tests-f58def71b59a

还要检查您的迁移是否正常工作

Python3 manage.py makemigrations
Python3 manage.py migrate

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

class Meta:
    app_label  = 'name_of_my_app'