[Fixed]-RuntimeError: Model class xxx doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

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.

20👍

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.

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

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.

1👍

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.

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

Leave a comment