python - Django model "doesn't declare an explicit app_label"

ID : 20137

viewed : 18

Tags : pythondjangopython-3.xpython

Top 5 Answer for python - Django model "doesn't declare an explicit app_label"

vote vote

98

Are you missing putting in your application name into the settings file? The myAppNameConfig is the default class generated at apps.py by the .manage.py createapp myAppName command. Where myAppName is the name of your app.

settings.py

INSTALLED_APPS = [ 'myAppName.apps.myAppNameConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] 

This way, the settings file finds out what you want to call your application. You can change how it looks later in the apps.py file by adding the following code in

myAppName/apps.py

class myAppNameConfig(AppConfig):     name = 'myAppName'     verbose_name = 'A Much Better Name' 
vote vote

81

I get the same error and I donĀ“t know how to figure out this problem. It took me many hours to notice that I have a init.py at the same direcory as the manage.py from django.

Before:

|-- myproject   |-- __init__.py  <---   |-- manage.py   |-- myproject     |-- ...   |-- app1     |-- models.py   |-- app2     |-- models.py 

After:

|-- myproject   |-- manage.py   |-- myproject     |-- ...   |-- app1     |-- models.py   |-- app2     |-- models.py 

It is quite confused that you get this "doesn't declare an explicit app_label" error. But deleting this init file solved my problem.

vote vote

80

I had exactly the same error when running tests with PyCharm. I've fixed it by explicitly setting DJANGO_SETTINGS_MODULE environment variable. If you're using PyCharm, just hit Edit Configurations button and choose Environment Variables.

Set the variable to your_project_name.settings and that should fix the thing.

It seems like this error occurs, because PyCharm runs tests with its own manage.py.

vote vote

64

I got this one when I used ./manage.py shell then I accidentally imported from the root project level directory

# don't do this from project.someapp.someModule import something_using_a_model # do this from someapp.someModule import something_using_a_model  something_using_a_model() 
vote vote

50

as a noob using Python3 ,I find it might be an import error instead of a Django error

wrong:

from someModule import someClass 

right:

from .someModule import someClass 

this happens a few days ago but I really can't reproduce it...I think only people new to Django may encounter this.here's what I remember:

try to register a model in admin.py:

from django.contrib import admin from user import User admin.site.register(User) 

try to run server, error looks like this

some lines... File "/path/to/admin.py" ,line 6 tell you there is an import error some lines... Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label 

change user to .user ,problem solved

Top 3 video Explaining python - Django model "doesn't declare an explicit app_label"

Related QUESTION?