Reputation: 956
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.
Upvotes: 14
Views: 35266
Reputation: 21
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!
Upvotes: 0
Reputation: 21
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.
Upvotes: 2
Reputation: 2551
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
Upvotes: 0
Reputation: 2701
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.
Upvotes: 1
Reputation: 121
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
Upvotes: 2
Reputation: 12849
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.
Upvotes: 23
Reputation: 956
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.
Upvotes: 17