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

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'

当前回答

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

其他回答

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

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

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运行测试时,我遇到了完全相同的错误。我已经通过显式设置DJANGO_SETTINGS_MODULE环境变量来修复它。如果您正在使用PyCharm,只需点击编辑配置按钮并选择环境变量。

将变量设置为your_project_name。设置和那应该解决问题。

似乎出现了这个错误,因为PyCharm使用自己的manage.py运行测试。

在我的例子中,发生这种情况是因为我在项目级urls.py、INSTALLED_APPS和apps.py中使用了相对模块路径,而不是在项目根目录中。即绝对模块路径贯穿始终,而不是相对模块路径+ hacks。

无论我在我的应用程序中的INSTALLED_APPS和apps.py中的路径有多混乱,我都无法让runserver和pytest同时工作,直到它们都根植于项目根目录中。

文件夹结构:

|-- manage.py
|-- config
    |-- settings.py
    |-- urls.py
|-- biz_portal
    |-- apps
        |-- portal
            |-- models.py
            |-- urls.py
            |-- views.py
            |-- apps.py

下面,我可以运行manage.py runserver和gunicorn与wsgi和使用门户应用程序视图没有麻烦,但pytest会错误与ModuleNotFoundError:没有模块命名为'apps'尽管DJANGO_SETTINGS_MODULE配置正确。

配置/ settings.py:

INSTALLED_APPS = [
    ...
    "apps.portal.apps.PortalConfig",
]

biz_portal / apps / portal / apps . py:

class PortalConfig(AppConfig):
    name = 'apps.portal'

配置/ urls . py:

urlpatterns = [
    path('', include('apps.portal.urls')),
    ...
]

将config/settings.py中的app引用更改为biz_portal.apps.portal.apps。PortalConfig和PortalConfig.name到biz_portal.apps。门户允许pytest运行(我还没有对门户视图进行测试),但runserver会出错

模型类apps.portal.models.Business没有显式声明app_label,也不在INSTALLED_APPS中的应用程序中

最后,我为应用程序做了准备。门户查看哪些仍然使用相对路径,并发现config/urls.py也应该使用biz_portal.apps.portal.urls。

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