Django 1.9 deprecation warnings app_label
I've just updated to Django v1.8, and testing my local setup before updating my project and I've had a deprecation warning that I've never seen before, nor does it make any sense to me. I may be just overlooking something or misunderstanding the documentation.
/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:6: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Difficulty doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Difficulty(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:21: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Zone doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Zone(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:49: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Boss doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Boss(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/raiding/models.py:79: RemovedInDjango19Warning: Model class ankylosguild.apps.raiding.models.Item doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Item(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:14: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Category doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Category(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:36: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Comment doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Comment(ScoreMixin, ProfileMixin, models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:64: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Forum doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Forum(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:88: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.Post doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Post(ScoreMixin, ProfileMixin, models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:119: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.CommentPoint doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class CommentPoint(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/forum/models.py:127: RemovedInDjango19Warning: Model class ankylosguild.apps.forum.models.TopicPoint doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class TopicPoint(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/auctionhouse/models.py:10: RemovedInDjango19Warning: Model class ankylosguild.apps.auctionhouse.models.Auction doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Auction(models.Model):
/Users/neilhickman/Sites/guild/ankylosguild/apps/auctionhouse/models.py:83: RemovedInDjango19Warning: Model class ankylosguild.apps.auctionhouse.models.Bid doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Bid(models.Model):
Now this poses 3 questions for me.
- According to the documentation,
Options.app_label
isn't a requirement unless the model is outside of the application module, which in my case, it isn't. Secondly, this behaviour was deprecated in 1.7 anyway, so why is it even an issue? - The applications are all in the INSTALLED_APPS tuple, so it surely can't be that?
- Why would the applications not be loaded before they are called if everything is in the INSTALLED_APPS tuple?
If I am indeed doing something wrong, what is the correct way of doing it as the docs don't really clear up what is causing this problem or how to rectify it.
Solution 1:
As stated in the warning, this happens either :
- When you're using a model which is not in an
INSTALLED_APPS
; - Or when you're using a model before its application is loaded.
Since you did refer the app in the INSTALLED_APPS
setting, this is most likely you're using a model before the app initialisation.
Typically, this occurs when you have from .models import SomeModels
in an apps.py early signal (for example post_migrate
).
Instead of referring your models the classic way here, it is recommended to use AppConfig.get_model().
Check your apps.py file for any model import, and replace them using this api.
For example instead of :
# apps.py
from django.apps import AppConfig
from .models import MyModel
def do_stuff(sender, **kwargs):
MyModel.objects.get() # etc...
class MyAppConfig(AppConfig):
name = 'src.my_app_label'
def ready(self):
post_migrate.connect(do_stuff, sender=self)
Do this :
# apps.py
from django.apps import AppConfig
def do_stuff(sender, **kwargs):
mymodel = sender.get_model('MyModel')
mymodel.objects.get() # etc...
class MyAppConfig(AppConfig):
name = 'src.my_app_label'
def ready(self):
post_migrate.connect(do_stuff, sender=self)
Note this enforcement was introduced in bug #21719.
Solution 2:
Similar error. In my case the error was:
RemovedInDjango19Warning: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Site(models.Model):
My solution was:
Added 'django.contrib.sites'
to INSTALLED_APPS
Solution 3:
I suspect it'll be only a tiny minority of people who see this error for whom it will be caused by the same thing as me, but in case it helps someone else it seems worth adding this answer!
I suddenly saw lots of these errors when running tests at one point - it turned out that I had accidentally created a __init__.py
at the top level of my Django project when it was meant to be in a subdirectory. The clue that this was happening is that the errors, which were like:
/home/mark/mystupiddjangoproject/alerts/models.py:15: RemovedInDjango19Warning: Model class mystupiddjangoproject.alerts.models.Alert doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
class Alert(models.Model):
... included the name of directory the project is in (mystupiddjangoproject
) in the fully qualified model name, which should have been: alerts.models.Alert
.
To fix this, I just needed to do:
rm __init__.py
rm __init__.pyc
Solution 4:
I was getting a similar error, but rather than complaining about models in my apps, it was complaining about models in contrib
packages. For example:
C:\Program Files\Python 2.7\lib\site-packages\django\contrib\sessions\models.py:27: RemovedInDjango19Warning: Model class django.contrib.sessions.models.Session doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9. class Session(models.Model):
This is caused by a bad ordering in the INSTALLED_APPS
property in settings.py
. My settings.py
initially contained:
INSTALLED_APPS = (
'my_app_1',
'my_app_2',
'my_app_3',
'bootstrap_admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'social.apps.django_app.default',
'mathfilters',
'rest_framework',
)
my_app_*
use models from the contrib
packages. The error is caused by using models before declaring them (i.e. Django
should know about the apps containing those models before using them).
In order to solve this, the order in which the apps are declared needs to be changed. Specifically, all Django apps should come before the user defined apps. In my case, the correct INSTALLED_APPS
would look like:
INSTALLED_APPS = (
'bootstrap_admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'social.apps.django_app.default',
'mathfilters',
'rest_framework',
'my_app_1',
'my_app_2',
'my_app_3',
)
Now I know this might not directly answer your question, but it answers a related one, and since this is the only SO link that shows on Google when pasting the error, I have answered it here.
However, I believe a similar situation is causing your problem:
Make sure you declare the "dependency" apps before the apps using them! The errors don't really specify which app is using a model, so you would have to push the app containing the model it mentions to the top, one by one, until the error disappears.
Solution 5:
Add a meta class to your model with an app_label attribute.
class Meta:
app_label = 'app_model_belongs_to'
Hope this works!
EDIT: The reason for this is usually that the model exists outside of the standard locations.
For more information refer to: https://docs.djangoproject.com/en/1.8/ref/models/options/#app-label