13

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.

Philipp
  • 794
  • 1
  • 7
  • 21

6 Answers6

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.

markwalker_
  • 12,078
  • 7
  • 62
  • 99
  • 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. – Philipp Apr 06 '19 at 22:58
  • 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. – markwalker_ Apr 07 '19 at 10:23
  • 2
    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](https://github.com/pydanny/cookiecutter-django/issues/1876)). – Philipp Apr 08 '19 at 19:55
  • 2
    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. – yǝsʞǝla Dec 10 '19 at 02:50
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.

Philipp
  • 794
  • 1
  • 7
  • 21
  • 3
    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. – yǝsʞǝla Dec 10 '19 at 02:51
  • 1
    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? – Zach Aug 21 '20 at 03:22
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

RobC
  • 22,977
  • 20
  • 73
  • 80
John Burrows
  • 111
  • 1
  • 1
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.

dKen
  • 3,078
  • 1
  • 28
  • 37
Mujeeb Ishaque
  • 2,259
  • 24
  • 16
  • 1
    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](https://github.com/Apfelschuss/apfelschuss/tree/master/config/settings). 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. – Philipp Apr 26 '19 at 13:23
  • 1
    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. – Mujeeb Ishaque Apr 27 '19 at 12:37
  • 1
    Ah, now I got it :-) I am sure it will help others. Thanks for clarification Mujeeb. – Philipp Apr 27 '19 at 14:17
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.

Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
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
Edward D'Souza
  • 2,453
  • 1
  • 26
  • 24