votes up 1

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

Package:
django
github stars 59414
Exception Class:
RuntimeError

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)
                    )

            else:
                app_label = app_config.label

        new_class.a
😲 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 models.py file in our app while the app name is not added in the settings.py 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 manage.py startapp testapp

At this point you should have the following folder structure

.
├── manage.py
├── newProject
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── testapp
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

Then add the following codes in each respective file.

testapp/models.py

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)

testapp/views.py

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/tests.py for example.

newProject/urls.py

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

Now start the server

$ python3 manage.py runserver     

and go to this url on your browser

http://127.0.0.1:8000/home/

The following error should be seen on the terminal

File "/Users/kellem/Projects/Others/fixexception/django/newProject/newProject/urls.py", line 18, in <module>
    from testapp.views import homePageView
  File "/Users/kellem/Projects/Others/fixexception/django/newProject/testapp/views.py", line 2, in <module>
    from testapp.models import Users
  File "/Users/kellem/Projects/Others/fixexception/django/newProject/testapp/models.py", line 6, in <module>
    class Users(models.Model):
  File "/Users/kellem/.local/share/virtualenvs/django-oqVKUupH/lib/python3.9/site-packages/django/db/models/base.py", 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/apps.py notice the default class generated automatically.

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

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '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