Django model “doesn't declare an explicit app_label”


Django model “doesn't declare an explicit app_label”



I'm at wit's end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:


Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label



There is SO LITTLE info on this on the web, and no solution out there has resolved my issue. Any advice would be tremendously appreciated.



I'm using Python 3.4 and Django 1.10.



From my settings.py:


INSTALLED_APPS = [
'DeleteNote.apps.DeletenoteConfig',
'LibrarySync.apps.LibrarysyncConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]



And my apps.py files look like this:


from django.apps import AppConfig


class DeletenoteConfig(AppConfig):
name = 'DeleteNote'



and


from django.apps import AppConfig


class LibrarysyncConfig(AppConfig):
name = 'LibrarySync'





You don't have django.contrib.contenttypes in INSTALLED_APPS.
– RemcoGerlich
Oct 23 '16 at 18:45





Then the other likely thing is that you imported it before its models were loaded, is some app that is listed before contenttypes in INSTALLED_APPS using it?
– RemcoGerlich
Oct 23 '16 at 18:48





That's unusual, you have no project or app of your own at all?
– RemcoGerlich
Oct 23 '16 at 18:50





Everything that has a models.py should be in INSTALLED_APPS; and if one of them uses contenttype (because of a generic foreign key, say) then it needs to be under contenttypes in the list.
– RemcoGerlich
Oct 23 '16 at 18:59





Frustrating, it's likely to be something very small but hard to tell from here where. Do you import any of your stuff in settings.py or so?
– RemcoGerlich
Oct 23 '16 at 19:13




12 Answers
12



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.


myAppNameConfig



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'





Okay so this makes a lot of sense to me with the example, and I've implemented the changes now based on my understanding of the syntax, but I'm still hitting 100% the exact same error. I've updated my post to elaborate.
– Slbox
Oct 23 '16 at 19:09





I also tried putting my apps below the django.contrib ones, same message.
– Slbox
Oct 23 '16 at 19:11





Thanks to @xeberdee and @RemcoGerlich for their help with this. In the end, my solution was to load my apps below the django.contrib apps, and to move my entry of import django django.setup() in my settings.py, to beneath the INSTALLED_APPS entry.
– Slbox
Oct 23 '16 at 19:20


import django django.setup()


INSTALLED_APPS





Just out of curiosity - why import django django.setup() in the settings file? Also, your apps should load even if they are the first in the installed apps list.
– Xeberdee
Oct 25 '16 at 17:39






What's the difference between this and what he wrote in his question?
– Matt D
Jul 4 '17 at 1:15



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


./manage.py shell


# 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()



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.


DJANGO_SETTINGS_MODULE



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


your_project_name.settings



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


manage.py





Had this problem running Pycharm Tests although running server through Pycharm did not require me to add settings. Manually adding DJANGO_SETTINGS_MODULE to configuration for test solved if for me.
– PhoebeB
Apr 21 at 13:42



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


user


.user





Welcome to stackoverflow! I feel compelled to mention that your answer is unrelated to the OP's question. As a noob, you should be careful proposing solutions without first verifying their correctness. But please keep coming back, and post concrete answers when you can—thanks!
– evadeflow
Jul 6 '17 at 14:14





I wish more comments on Stack were like yours Xeon Phil. Too often new users are chased off by rabid commenters unhappy that they are not Stack experts on day one.
– Slbox
Jul 9 '17 at 15:55





You are right, @evadeflow , my first answer looks really unrelated, I try to clarify the answer.Anyway,just hope the answer might be useful.
– rpstw
Jul 11 '17 at 10:08





Thank you, @Slapbox , your words really make me feels good!
– rpstw
Jul 11 '17 at 10:10





It was a similar issue in my case. 'from ..core.models import CommonInfo' had to become 'from apps.core.models import CommonInfo'
– user42488
Apr 25 at 11:53



I had the same problem just now. I've fixed mine by adding a namespace on the app name. Hope someone find this helpful.



apps.py


from django.apps import AppConfig

class SalesClientConfig(AppConfig):
name = 'portal.sales_client'
verbose_name = 'Sales Client'



I got this error while trying to upgrade my Django Rest Framework app to DRF 3.6.3 and Django 1.11.1.



For anyone else in this situation, I found my solution in a GitHub issue, which was to unset the UNAUTHENTICATED_USER setting in the DRF settings:


UNAUTHENTICATED_USER


# webapp/settings.py
...
REST_FRAMEWORK = {
...
'UNAUTHENTICATED_USER': None
...
}



I received this error after I moved the SECRET_KEY to pull from an environment variable and forgot to set it when running the application. If you have something like this in your settings.py


SECRET_KEY


settings.py


SECRET_KEY = os.getenv('SECRET_KEY')



then make sure you are actually setting the environment variable.



I ran into this error when I tried generating migrations for a single app which had existing malformed migrations due to a git merge. e.g.


manage.py makemigrations myapp



When I deleted it's migrations and then ran:


manage.py makemigrations



the error did not occur and the migrations generated successfully.



Most probably you have dependent imports.



In my case I used a serializer class as a parameter in my model, and the serializer class was using this model:
serializer_class = AccountSerializer


from ..api.serializers import AccountSerializer

class Account(AbstractBaseUser):
serializer_class = AccountSerializer
...



And in the "serializers" file:


from ..models import Account

class AccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
fields = (
'id', 'email', 'date_created', 'date_modified',
'firstname', 'lastname', 'password', 'confirm_password')
...



I got this error on importing models in tests, i.e. given this Django project structure:


|-- myproject
|-- manage.py
|-- myproject
|-- myapp
|-- models.py # defines model: MyModel
|-- tests
|-- test_models.py



in file test_models.py I imported MyModel in this way:


test_models.py


MyModel


from models import MyModel



The problem was fixed if it is imported in this way:


from myapp.models import MyModel



Hope this helps!



PS: Maybe this is a bit late, but I not found in others answers how to solve this problem in my code and I want to share my solution.



I just ran into this issue and figured out what was going wrong. Since no previous answer described the issue as it happened to me, I though I would post it for others:


python migrate.py startapp myApp


mv myApp myFolderWithApps/


python migrate.py makemigrations


myFolderWithApps.myApp



So to make a long story short:
- the issue was initially coming from the wrong app name in apps.py of myApp, in settings and in the import path of my second app.
- but it was not enough to correct the paths in these three places, as migrations had been created with imports referencing the wrong app name. Therefore, the same error kept happening while migrating (except this time from migrations).



So... check your migrations, and good luck!



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?