starting new project - getting model 'doesn't declare an app_label'

Hi all,

I’m starting a new django project which I haven’t done in a while and I was imagining building a fairly tightly coupled htmx-models, real modern and simple, I was planning to be so clever.

At the moment, I am stuck because my project hits this error:

"RuntimeError: Model class pysrc.floor.models.Activity doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."

The model in question is not complicated and is in floor/models.py:

class Activity(models.Model):
    name = models.CharField(max_length=250)
    precedence = models.PositiveIntegerField(default=1)
    target_groups = models.ManyToManyField(Group)
    parent_activity = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)

The ‘floor’ app folder was created with startapp; I’ve added floor.apps.FloorConfig to INSTALLED_APPS and tried it at both ends.

INSTALLED_APPS = [
    "floor.apps.FloorConfig",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

Weird stuff that I did/am doing that I don’t think should matter:

  • I changed the folder structure to put the django folders under a folder called ‘pysrc’ instead of the project name (‘deep’) - so the settings are in deep.settings, or deep.pysrc.deep.settings in full, and models are in deep.pysrc.floor.models. manage.py is in the pysrc folder: ie
deep
|-- pysrc
    |--deep
        |--- settings.py, asgi.py etc
    |--floor
        |--- models.py, apps.py etc
    manage.py
|-- non-py folders (future docker stuff)
  • I am experimenting with creation of models (only at import time) using the type() function (later in the script, not the Activity model)
  • I am triggering this error primarily from a test in floor/tests.py (it does also occur for makemigrations etc)

This is the problem. Look at manage.py, in the line 9 it expect you hace your settings file in “config.settings” you should change that. In settings.py look for BASE_DIR, maybe you have to add a “.parent” to the path.

manage.py sets DJANGO_SETTINGS_MODULE = “deep.settings”, which remains a correct relative path to the settings, and when I put a ‘print(BASE_DIR)’ in the settings.py, it prints the full path to the pysrc folder. I added a print(“Floor config visited”) to floor.apps and it prints.

But you were right that pysrc.floor.models was the problem - I had an __init__.py in the ‘pysrc’ folder marking it as a module, and deleting it has apparently corrected the problem.