J. Santiago
J. Santiago

Reputation: 139

Django "Model class <Class> doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS" after splitting models

I'm getting this error and I've been trying to fix it for many hours, it came up once I separated my models.py into a directory with multiple files.

This is the current structure of my project. I have omitted many files, but these are the relevant ones:

Before, I had all my models in a single file inside app2, called models.py. However, as the file started to grow too large, I separated each of the models into different categories. Also, in __init__.py I imported these elements:

from academy.app2.models.content import *
from academy.app2.models.session import *

Now, when I try to make migrations with python manage.py makemigrations app2, as usual, I'm getting this error:

RuntimeError: Model class app2.models.content.Reward doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

When I searched for this error, I came across this answer, however, when I add the Meta Class declarating the app_label, I get this error:

RuntimeError: Conflicting 'reward' models in application 'app2': <class 'academy.app2.models.content.Reward'> and <class 'app2.models.content.Reward'>.

This is my Config file for app2:

class App2Config(AppConfig):
    name = 'academy.app2'

This is my INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'academy.app2.apps.App2Config',
    ...
]

This error did not occur when I had a single models.py file, but when I separated it into separate files. I also tried to create an additional base model, and define in that model the Meta Class, inheriting in the rest of the models, without success.

Upvotes: 1

Views: 6006

Answers (1)

J. Santiago
J. Santiago

Reputation: 139

After many attempts, I found the solution to my problem:

First, I edited __init.py__ to the following syntax:

from .content import *
from .session import *
from .system import *

In addition, I updated the imports from one file to another, from the app2.models.file format to this: academy.app2.models.file.

It was not necessary to add a base class. I was now able to run the migrations correctly. I hope this is helpful to someone.

Upvotes: 3

Related Questions