99

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!

Michael Scheper
  • 5,977
  • 7
  • 53
  • 74
darkhorse
  • 7,008
  • 15
  • 53
  • 120
  • Does this answer your question? [Django model "doesn't declare an explicit app\_label"](https://stackoverflow.com/questions/40206569/django-model-doesnt-declare-an-explicit-app-label) – Qumber Nov 26 '20 at 10:09
  • I found that the problem stemmed from not including an app definition in `settings.py` for a newly created app. After adding `'.apps.'` to `INSTALLED_APPS`, Django understands its a registered application. – Tanner Dolby Aug 26 '21 at 16:16

6 Answers6

258

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 .

Ian Price
  • 7,122
  • 2
  • 20
  • 33
  • 1
    It fixes the problem, but where does it come from? – erikbstack Aug 23 '16 at 15:20
  • 1
    [Django's Sites Framework](https://docs.djangoproject.com/en/1.10/ref/contrib/sites/) 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). In this particular case [AllAuth requires the Sites Framework](http://django-allauth.readthedocs.io/en/latest/installation.html) in order to function properly (as do many other third-party libraries, built to safely handle cases where multiple sites may be present). – Ian Price Aug 23 '16 at 15:46
  • in 2018 you don't need SITE_ID anymore, and yet I have this problem – holms Mar 02 '18 at 10:49
  • 10
    Despite having 'django.contrib.sites' and SITE_ID = 1, I still have this problem. – Ross Symonds Oct 28 '20 at 13:20
  • Check traceback and you will see which 3rd party app is trying to use django.contrib.sites. In my case is userena. – ming Jul 30 '21 at 00:19
  • In case it helps someone, I had both 'django.contrib.sites' in INSTALLED_APPS and SITE_ID = 1, but still got the error, so I changed the order of the apps, moved the newly added app to the bottom, and it worked. – Arislan Makhmudov May 24 '22 at 06:10
8

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

berto
  • 7,757
  • 4
  • 24
  • 21
  • Had a similar issue but running Python3. In my tests.py file I was using a relative import of a view I wanted to test `from .views import LatestAccount`. The error I was getting wasn't even for a model used in the tests.py file. Yet removing that relative import and replacing it with an absolute import fixed the issue. – user1847 Oct 31 '17 at 01:20
  • @ColtonHicks what test runner did this happen with; I'll amend your python3 findings to my answer. – berto Dec 18 '17 at 19:51
  • I was using pytest 3.2.3 inside a django 1.11.4 app. – user1847 Dec 19 '17 at 00:33
2

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',
]
garg10may
  • 5,128
  • 8
  • 42
  • 78
hum3
  • 1,333
  • 13
  • 17
2

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

Mihai Chelaru
  • 6,705
  • 14
  • 41
  • 48
Ahmed Adewale
  • 2,615
  • 21
  • 17
2

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

class Meta:

    app_label = 'yourApp'
PowerAktar
  • 2,111
  • 1
  • 18
  • 15
  • Mine only shows a different error ` RuntimeError: Conflicting 'uploadedcsvfile' models in application 'odin': and .` odin being the `yourApp` name and UploadedCSVFile being the model name. Would you happen to know why? – AlphaCR Sep 27 '21 at 09:57
  • Its difficult to say without seeing your code. But from the error, I would think that you have multiple models with the same name? – PowerAktar Sep 30 '21 at 11:33
2

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.

Panky
  • 21
  • 2