RuntimeError: Model class xxx doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
Asked Answered
B

7

14

I refer to following GitHub repo which is based on Django 2.0 and cookiecutter-django: github.com/Apfelschuss/apfelschuss/tree/c8851430201daeb7d1d81c5a6b3c8a639ea27b02

I am getting the following error when trying to run the app:

RuntimeError: Model class votes.models.Author doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Error appeared with this line of code.

I tried to do as described in https://mcmap.net/q/115944/-django-model-quot-doesn-39-t-declare-an-explicit-app_label-quot but without success:

config/settings/base.py

LOCAL_APPS = [
    "apfelschuss.votes.apps.VotesConfig"
]

apfelschuss/votes/apps.py

from django.apps import AppConfig


class VotesConfig(AppConfig):

    name = "apfelschuss.votes"
    verbose_name = "Votes"

Any idea what I am doing wrong?

If anyone is interested how to run the docker container of the repo. It is described here.

Burgener answered 6/4, 2019 at 20:35 Comment(0)
B
17

Working with absolute imports in the view solved my issue. I changed .models to apfelschuss.votes.models.

Code that leads to runtime error:

from django.shortcuts import render

from .models import Voting

Issue solved with absolute import:

from django.shortcuts import render

from apfelschuss.votes.models import Voting

See commit on GitHub here.

Burgener answered 7/4, 2019 at 17:11 Comment(2)
Correct answer is to add Meta. Absolute imports don't do anything if you models and not in the same directory/package as the app.Kvass
Absolute import worked for me with no other changes to the model. I would have preferred relative import. Is there a way to do this?Eichman
U
22

When it says "Model class xxx doesn't declare an explicit app_label" your models can specify Meta to define their app_label. You can also customise the database table name along with a bunch of other options as part of the meta data.

You need to do something like this on all your models;

class Author(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_picture = models.ImageField()

    class Meta:
        app_label = 'apfelschuss.votes'

    def __str__(self):
        return self.user.username

edit

I've checked out your repo & I think you're over-complicating the project by having the users and votes apps under apfelschuss.

I pulled them out to the root of the project & everything runs smoothly; https://github.com/marksweb/apfelschuss/tree/so/questions/55553252

This is a more typical approach to project structure in django/python projects.

Unrighteous answered 6/4, 2019 at 22:19 Comment(4)
Thank you for your appreciated feedback Mark. Actually, this model is not defined outside of INSTALLED_APPS, right? Sorry, I am new to python. Nevertheless I added meta as described but unfortunately I got ValueError: Invalid model reference 'apfelschuss.votes.Voting_categories'. String model references must be of the form 'app_label.ModelName'. I tried also some other strings without success.Burgener
Yeah technically it's not needed, but that's what your error is telling you. I think app_label on models doesn't support that dotted path approach - I was trying to illustrate the concept more than anything. Also, I am running your app in django 2.2 without issue. I'll have a go at your full project & docker, but the apfelschuss.votes.apps.VotesConfig app loads & installs.Unrighteous
Thank you for your appreciated feedback Mark. Since I use cookiecutter-django as skeleton I prefer having the apps in the second level apfelschuss folder (see #1876).Burgener
THIS SHOULD BE THE ACCEPTED ANSWER! In my case I moved models out to another place and had to add Meta like you specified to make it work. Thanks for the link too. Changing relative to absolute imports like suggested in the other answer does not help - I was already using absolute paths and had that issue.Kvass
B
17

Working with absolute imports in the view solved my issue. I changed .models to apfelschuss.votes.models.

Code that leads to runtime error:

from django.shortcuts import render

from .models import Voting

Issue solved with absolute import:

from django.shortcuts import render

from apfelschuss.votes.models import Voting

See commit on GitHub here.

Burgener answered 7/4, 2019 at 17:11 Comment(2)
Correct answer is to add Meta. Absolute imports don't do anything if you models and not in the same directory/package as the app.Kvass
Absolute import worked for me with no other changes to the model. I would have preferred relative import. Is there a way to do this?Eichman
G
2

I'm using Python 3.7.5 on VS Code. This same issue was confusing me. I went into the initially created project and found settings.py

Went to the section

INSTALLED_APPS = []

and added

'myapp.apps.MyappConfig', - make sure it's cased correctly

this refers to the class in apps.py in the app causing issues

Gibraltar answered 11/4, 2020 at 9:50 Comment(0)
E
2

In file apps.py we see:

class ArticlesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'Django.apps.articles'

We need name 'Django.apps.articles'

Now write in terminal:

from Django.apps.articles.models import Article

And all working! I ran into this problem in PyCharm.

Ecbolic answered 6/2, 2022 at 14:43 Comment(0)
S
1

You have accidentally added your app name under MIDDLEWARE section in settings.py.

Spent some good time debugging, thought this might help save someone else's time.

Statant answered 24/4, 2019 at 20:13 Comment(3)
Thank you for your feedback and troubleshooting Mujeeb. Honestly I am not quit sure where you are referring to? I have no settings.py since I work with Cookiecutter setup. See here. The installed apps in base.py are INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS but I did not add my app to the middleware.Burgener
Thank you, Philpp. So, I got the same error as you and the mistake was as mentioned above. The answer certainly is not for you but for some other person, coming in future maybe, facing the same issue. If it doesn't help in any way, let me know and I will delete it. Thank you.Statant
Ah, now I got it :-) I am sure it will help others. Thanks for clarification Mujeeb.Burgener
C
0

I had the same error and fixed it by adding a missing __init__.py file (just a blank file) to my main module inside my project root.

~/my_project
    foo/
        models.py
        tests.py
        __init__.py  # <-- Added an empty __init__.py here
Cameroncameroon answered 6/7, 2021 at 15:57 Comment(0)
U
0

Landed up here on a google search.
For others facing a similar error:
Context:Upgrading a Django 2.1 app to Django 5.0.
Solution:Check for the import statements for your Classes

For me, The error popped up when the class yyyy in apps/xxxx/models.py was imported in the apps/xxxx/admin.py.

from apps.xxxx.models import yyyy  # <-- Changed this to 
from .models import yyyy           # <-- a relative import
                                   #     inside the app xxxx 

:o: Oddly, Philipp's Solution is exactly the opposite to what I needed to do. Py3.10 ( via pyenv ) + Django 5 on MacoS Does that really impact anything ?


I faced this problem when upgrading an app on Django 2.1 to Django 5 as an experiment, after fixing up the dependencies on starting the app,

RuntimeError: Model class apps.xxxx.models.yyyy doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS. started to pop up.

In the working Django 2 xxxx app the imports in admin.py looked like:

from apps.xxxx.models import yyyy

Recent examples for Django stated to import the models as a relative import in the following fashion:

from .models import yyyy

Cannot yet comment so still pushing in answers!

Underworld answered 22/3 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.