When building a Django application, you may encounter the error “Model class
<model name> doesn’t declare an explicit app_label and isnt in an application in INSTALLED_APPS.” This error occurs when you define a model outside of an installed app, causing Django to be unable to associate the model with an application. Fortunately, this error is easy to fix with a small change to your model definition.
Understanding Django Apps and Models
First, it’s helpful to understand how Django organizes code into apps and models. In Django, an app is a Python package that provides specific functionality for a project. The INSTALLED_APPS setting contains a list of installed apps that Django can import models and other code from.
Models define the structure of data in a Django project. They are Python classes that subclass django.db.models.Model. By default, Django expects models to be defined inside an installed app, which allows it to identify which app each model belongs to.
Why the Error Occurs
When you define a model outside of an installed app, Django has no way to determine which app it belongs to. This results in the “doesn’t declare an explicit app_label” error.
Some common ways this can happen include:
- Defining models directly in the project s models.py file rather than within an app
- Placing models within a folder that is not a properly configured Django app
- Forgetting to add the app containing a model to INSTALLED_APPS
Fixing with app_label
The easiest way to fix this error is to explicitly specify the app_label for the model. This can be done by adding an app_label attribute to the model’s Meta class.
from django.db import models
# fields here
app_label = 'myapp'
Where ‘myapp’ is the name of the app the model belongs to.
This explicitly tells Django which app the model is part of, resolving the issue.
Fixing Error with an App Directory
Another way is to move the model inside one of your project’s installed apps.
For example, you might create an app called ‘main’ or ‘utils’ to hold reusable models not specific to any other app. Add this new app to INSTALLED_APPS.
Then move the models inside this app directory and structure it like a normal Django app:
With the model defined inside an installed app, Django can now associate it with that app and resolve the error.
When to Use Each Approach
Using app_label is quick and doesn’t require any file moves. But it can make sense to group models within apps that relate to each other.
- Use app_label for one-off models not tied to any app
- Put models that relate to other models within the same app
- Create a shared app like ‘utils’ for reusable models
The “Django model doesnt declare an explicit app_label” points to an easy mistake when defining models. Fortunately, it’s simple to resolve by either using the app_label meta attribute or moving the model inside an installed application’s directory. With an awareness of how Django associates models and apps, you can avoid this error and structure your project cleanly.