我真是一筹莫及。经过十几个小时的故障排除,可能更多,我以为我终于可以做生意了,但接着我发现:
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')
...
在我的例子中,我在将代码从Django 1.11.11移植到Django 2.2时得到了这个错误。我正在定义一个自定义的FileSystemStorage派生类。在Django 1.11.11中,我在models.py中有如下一行:
from django.core.files.storage import Storage, DefaultStorage
然后在文件中我有类定义:
class MyFileStorage(FileSystemStorage):
然而,在Django 2.2中,我需要在导入时显式引用FileSystemStorage类:
from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage
瞧!,错误消失。
注意,每个人都在报告Django服务器吐出的错误消息的最后一部分。然而,如果你向上滚动,你会在错误的中间找到原因。
我在测试中导入模型时遇到了这个错误,即给出这个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:也许这有点晚了,但我在其他人的答案中没有发现如何解决我的代码中的这个问题,我想分享我的解决方案。