Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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://stackoverflow.com/a/40206661/5894988 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.

like image 597
Philipp Avatar asked Apr 06 '19 20:04

Philipp


2 Answers

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.

like image 193
Philipp Avatar answered Nov 20 '22 10:11

Philipp


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.

like image 17
markwalker_ Avatar answered Nov 20 '22 09:11

markwalker_