107

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
  • 6,514
  • 7
  • 63
  • 76
darkhorse
  • 8,192
  • 21
  • 72
  • 148
  • 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

11 Answers11

265

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,416
  • 2
  • 23
  • 34
  • 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
  • 15
    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
9

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
  • 8,215
  • 4
  • 25
  • 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
3

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

class Meta:

    app_label = 'yourApp'
PowerAktar
  • 2,341
  • 1
  • 21
  • 17
  • 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
3

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
  • 31
  • 2
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,794
  • 11
  • 50
  • 91
hum3
  • 1,563
  • 1
  • 14
  • 21
1

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

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
Ahmed Adewale
  • 2,943
  • 23
  • 19
1

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

KingNonso
  • 712
  • 7
  • 7
1

Django 4.1+ (2023)

After almost an hour digging, what solved for me was this:

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

No need for SITE_ID or additional INSTALLED_APPS entries.

Bersan
  • 1,032
  • 1
  • 17
  • 28
1

This error message typically occurs when Django encounters a model class that doesn't have an explicit app_label defined and is not included in the INSTALLED_APPS list in your Django project's settings.

To resolve this issue, you can follow these steps:

  1. Open your Django project's settings file (settings.py).

  2. Locate the INSTALLED_APPS list, which should contain the names of all the installed apps in your project.

  3. Make sure that the 'django.contrib.sessions' app is included in the INSTALLED_APPS list.

    INSTALLED_APPS = [
        # ...
        'django.contrib.sessions',
        # ...
    ]
    

If you find that 'django.contrib.sessions' is missing, add it to the list as shown above and save the file. If the app is already included in the INSTALLED_APPS list and you still encounter the error, there might be another issue with your project's configuration. In that case, it's worth verifying that you have the correct versions of Django and its dependencies installed, as compatibility issues can sometimes cause such errors.

Additionally, if you have custom model classes that don't have an explicit app_label defined, you should add the app_label attribute to those model classes. For example:

class Session(models.Model):
    # model fields
    class Meta:
        app_label = 'sessions'

By specifying the app_label attribute, you explicitly declare the app label for the model class, which helps Django locate and recognize it correctly. Replace 'sessions' with the appropriate label for your app.

Remember to restart your Django development server after making any changes to the settings file to ensure that the modifications take effect.

0

This error occurred because I had created a new app folder for a subset of sites related to another feature. This needed to be added to my INSTALLED_APPS in settings.py

merhoo
  • 589
  • 6
  • 18
-2

Everything worked as expected after I made a migration

python manage.py migrate

Good luck

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '22 at 09:10