RuntimeError: 模型类 django.Contrib.sites.Model。 Site 没有声明一个显式的 app_label,也不在 INSTALLED_APPS 中的应用程序中

我正在构建一个应用程序与 Django 休息框架和 AngularJ。我正在使用 Django-rest-auth 进行身份验证,尽管我还不能设置它。无论如何,我试图建立这个 应用程序与我的项目。我意识到我需要安装 django-rest-auth-register 来让它运行,所以我按照这个 文件来做以下事情:

我执行了命令

Pip install django-rest-auth

还有

Pip 安装 django-allauth

我的 设置是这样的:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',


# 3rd party apps
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',


# My app
'myapp',
]

我还添加了身份验证后端、 context _ 處理器和适当的 url。

但是,当我尝试迁移时,我的终端会抛出以下错误:

RuntimeError: 模型类 django 声明一个显式的 app _ label 并且不在 已安装的应用程序。

为什么我会得到这个错误,以及如何解决它来迁移我的项目? 谢谢!

106355 次浏览

The fix

Just add Django's Sites framework to your apps and set SITE_ID to 1 in your settings.

INSTALLED_APPS = [
...
'django.contrib.sites',
]


SITE_ID = 1

Why does this happen?

Django's Sites Framework is a contributed module bundled with the core library that allows for the use of a single Django application/codebase with different sites (that can use different databases, logic in views, etc). The SITE_ID setting, as stated in the docs, "is used so that application data can hook into specific sites and a single database can manage content for multiple sites."

In this particular case AllAuth requires the Sites Framework in order to function properly. Many other third-party libraries are built to safely handle cases where multiple sites may be present and as such may be best .

I landed on this post via Google search. My problem was running tests that blew up with the error:

RuntimeError: Model class app.taxonomy.models.Term doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

This was running on Python 2.7.x with absolute imports. As mentioned by Colton Hicks in the comments, below, this can also happen with Python 3 (pytest 3.2.3 with Django 1.11.4).

In my tests.py:

from __future__ import absolute_import
[...]
from .models import Demographics, Term

After changing the relative import to an absolute import the problem went away:

from taxonomy.models import Demographics, Term

HTH

I got the error above. However my problem was the in the urls.py. I was following PyDanny cookiecutter django recipe. My error was to put in the urls.py this line:

    url(r'^demo/', include('project.demoapp.urls', namespace='demoapp')),

when I corrected to this:

    url(r'^demo/', include('demoapp.urls', namespace='demoapp')),

all was well. I also changed my local apps (I did this first and so the critical error was the url misconfiguration):

LOCAL_APPS = [
# Your stuff: custom apps go here
'demoapp.apps.DemoAppConfig',
]

Just add 'django.contrib.sites', to INSTALLED_APPS and set SITE_ID = 1 in your settings.py file.

Try adding the app_label = 'yourApp' in the models Meta class:

class Meta:


app_label = 'yourApp'

I have django debug toolbar installed and this was actually causing the/my problem. INSTALLED_APPS (in settings.py) needs the entry 'django.contrib.sessions'. Make sure to run migrate after adding.

Everything worked as expected after I made a migration

python manage.py migrate

Good luck

Upgraded Answer for Django>=4.0 // 2022

Add Django's Sites framework and FlatPages Framework to your INSTALLED_APPS and set SITE_ID in your settings.

INSTALLED_APPS = [
...
'django.contrib.sites',
'django.contrib.flatpages',


]


SITE_ID = 1

Your tests should work like a charm