RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

DjangoDjango Rest-FrameworkDjango Rest-Auth

Django Problem Overview


I am building an application with Django Rest Framework and AngularJs. I am using Django-rest-auth for my authentication purposes, although, I have not been able to set it up. Anyway, I am trying to set up this app with my project. I realized I need to install django-rest-auth-registration to get it running, so I followed this documentation to do the following things:

I ran the commands

> pip install django-rest-auth

and

> pip install django-allauth

Any my settings.py looks like this:

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',
]

I have also added the authentication backends, context_processors, and the proper urls.

However, when I try to migrate, my terminal throws the following error:

> RuntimeError: Model class django.contrib.sites.models.Site doesn't > declare an explicit app_label and isn't in an application in > INSTALLED_APPS.

Why do I get this error, and how do I solve it to migrate my project? Thanks!

Django Solutions


Solution 1 - Django

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 .

Solution 2 - Django

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

Solution 3 - Django

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',
]

Solution 4 - Django

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

Solution 5 - Django

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

class Meta:

    app_label = 'yourApp'

Solution 6 - Django

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondarkhorseView Question on Stackoverflow
Solution 1 - DjangoIan PriceView Answer on Stackoverflow
Solution 2 - DjangobertoView Answer on Stackoverflow
Solution 3 - Djangohum3View Answer on Stackoverflow
Solution 4 - DjangoAhmed AdewaleView Answer on Stackoverflow
Solution 5 - DjangoPowerAktarView Answer on Stackoverflow
Solution 6 - DjangoPankyView Answer on Stackoverflow