votes up 1

Model class %s.%s doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

github stars 59414
Exception Class:

Raise code

an application configuration to attach the model to.
        app_config = apps.get_containing_app_config(module)

        if getattr(meta, 'app_label', None) is None:
            if app_config is None:
                if not abstract:
                    raise RuntimeError(
                        "Model class %s.%s doesn't declare an explicit "
                        "app_label and isn't in an application in "
                        "INSTALLED_APPS." % (module, name)

                app_label = app_config.label

😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 4 votes down

This exception is raised when the app name is not added to projects settings. When we try to import a model defined in the file in our app while the app name is not added in the file, this exception is raised immediately after the file where the model is imported gets called.

Here is

how to reproduce this exception:

$ pipenv install Django==4.0.2

$ django-admin startproject newProject

$ cd newProject

$ python3 startapp testapp

At this point you should have the following folder structure

├── newProject
│   ├──
│   ├──
│   ├──
│   ├──
│   └──
└── testapp
    ├── migrations
    │   └──

Then add the following codes in each respective file.


from django.db import models
# Create your models here.

class Users(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.SlugField(unique=True, max_length=255)


from django.shortcuts import render
# The Users class is not being used. It is imported to simulate the exception raising
from testapp.models import Users
from django.http import HttpResponse
# Create your views here.

def homePageView(request):
    return HttpResponse("Hello, World!")

As mentioned in the comment, The Users class imported just for the sake of reproducing the error. When the view is called the desired exception will be raised. Note that we could have used any other place as long as we are importing the Users class. It can be testapp/ for example.


from django.contrib import admin
from django.urls import path
from testapp.views import homePageView
urlpatterns = [
    path("home/", homePageView, name="home"),

Now start the server

$ python3 runserver     

and go to this url on your browser

The following error should be seen on the terminal

File "/Users/kellem/Projects/Others/fixexception/django/newProject/newProject/", line 18, in <module>
    from testapp.views import homePageView
  File "/Users/kellem/Projects/Others/fixexception/django/newProject/testapp/", line 2, in <module>
    from testapp.models import Users
  File "/Users/kellem/Projects/Others/fixexception/django/newProject/testapp/", line 6, in <module>
    class Users(models.Model):
  File "/Users/kellem/.local/share/virtualenvs/django-oqVKUupH/lib/python3.9/site-packages/django/db/models/", line 113, in __new__
    raise RuntimeError(
RuntimeError: Model class testapp.models.Users doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

This means we have reproduce the exception.At the same time the browser should display "Unable to connect" message.

How to fix the exception:

To fix this exception first go to testapp/ notice the default class generated automatically.

Then go to newProject/ and add the config to INSTALLED_APPS as follows.

    'testapp.apps.TestappConfig', # <-----------Notice this line

The app name is put as appname.apps.AppnameConfig

Now refresh the page on the browser and it should work fine.

Feb 08, 2022 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix