Django 1.9 app_label abandonment warnings

I just upgraded to Django v1.8 and tested my local setup before upgrading my project, and I got a warning that I had never seen before, and it makes no sense to me. I can just ignore something or misunderstand 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 for me there are 3 questions.

  • According to the documentation , Options.app_label not a requirement if the model is not outside the application module, which is not the case in my case. Secondly, this was still not approved in version 1.7, so why is this even a problem?
  • All applications are in the root directory of INSTALLED_APPS, so maybe this is not so?
  • Why applications are not loaded before they are called, if everything is in the root directory INSTALLED_APPS?

If I am really doing something wrong, this is the right way to do this, as the documents do not really clarify what causes this problem or how to fix it.

+51
python django deprecation-warning
Apr 14 '15 at 19:24
source share
12 answers

As indicated in the warning, this happens either:

  • When you use a model that is not in INSTALLED_APPS ;
  • Or when you use a model before its application is downloaded.

Since you referenced the application in the INSTALLED_APPS setting, it is most likely that you are using the model before initializing the application.

This usually happens when you have from .models import SomeModels in the early apps.py signal (e.g. post_migrate ). Instead of referencing your models in a classic way, it is recommended that you use AppConfig.get_model () . Check the apps.py file to import any model and replace them with 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 it:

 # 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 that this enforcement was introduced in error # 21719 .

+44
Apr 17 '15 at 15:14
source share

A 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

+50
Jul 12 '15 at 17:01
source share

I suspect that it will be just a small number of people who see this error, for which it will be caused by the same as me, but if it helps someone else, it seems worth adding this answer!

I suddenly saw a lot of these errors when running tests at some point - it turned out that I accidentally created __init__.py at the top level of my Django project, when it was supposed to be in a subdirectory. The hint that this happens is errors that were like this:

 /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): 

... the name of the directory in which the project is located ( mystupiddjangoproject ) is mystupiddjangoproject in the full name of the model, which should be: alerts.models.Alert .

To fix this, I just needed to do:

 rm __init__.py rm __init__.pyc 
+21
May 8 '16 at 10:31
source share

I was getting a similar error, but instead of complaining about the model in my applications, he complained about the model in contrib packages. For example:

C: \ Program Files \ Python 2.7 \ lib \ site-packages \ django \ contrib \ sessions \ models.py: 27: RemovedInDjango19Warning: the django.contrib.sessions.models.Session model class does not declare an explicit app_label line and either is not in INSTALLED_APPS application, or was imported before its application was downloaded. This will no longer be supported in Django 1.9. class Session (models.Model):

This is caused by incorrect ordering in the INSTALLED_APPS property in settings.py . My settings.py originally 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 contrib packages. The error is caused by the use of models before they are declared (i.e. Django needs to know about applications containing these models before ).

To solve this problem, you need to change the order in which applications are declared. In particular, all Django applications should appear in front of user applications . In my case, the correct INSTALLED_APPS will look like this:

 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 that this may not directly answer your question, but it answers the related one, and since this is the only SO link that appears on Google when I insert an error, I answered it here.

However , I believe that a similar situation causes your problem:

Make sure you declare “dependency” applications before applications using them! . Errors do not determine which application uses the model, so you will need to click on the application containing the model it mentions the top, one by one, until the error disappears.

+9
May 30 '15 at 9:32
source share

Add a metaclass to your model with the app_label attribute.

 class Meta: app_label = 'app_model_belongs_to' 

Hope it works!

EDIT: The reason for this is because the model exists outside of standard locations.

For more information, refer to: https://docs.djangoproject.com/en/1.8/ref/models/options/#app-label

+7
Apr 16 '15 at 16:55
source share

I ran into this problem and this was due to the way I loaded the signal.py module from the application.

As you can now say, quite often there are problems with the circulation between signals and models, and usually they need to be imported from the __init__.py application file or from the bottom of the models.py file to avoid them.

Well, I used the __init__.py approach, and only moving the import signals statement to the end of my models.py file solved the problem .

Hope this helps someone else!

+5
Nov 18 '15 at 8:43
source share

I had the same problem. I set a breakpoint in the django.db.models.base.py:line82 file and try to figure out what causes this warning.

 # Look for an application configuration to attach the model to. app_config = apps.get_containing_app_config(module) 

Basically, if your application does not exist by this time, you will receive this warning. I realized that my problem is that I have a third-party structure (in my case, haystack) that is trying to import one of my user models.

Perhaps you also have a third-party package specified in INSTALLED_APPS before your custom applications and your third-party package access your custom applications? This would be possible if you use something like a Django rest framework.

+4
Apr 28 '15 at 23:58
source share

The way Django 1.9 does this and giving your application a good name in the admin is as follows:

Add a file to your application called apps.py and add the following to it:

 #apps.py from django.apps import AppConfig class YourAppNameAppConfig(AppConfig): name = 'yourappname' verbose_name = 'Your App Name Looking Right' 

Then, in the __init__.py application file, add the following:

 #__init__.py default_app_config = 'youappname.apps.YourAppNameAppConfig' 
+4
Oct 26 '15 at 19:36
source share

I have this problem after upgrading Django from 1.8 to 1.9.1:

RuntimeError on /

The blog.models.BlogCategory model class does not declare an explicit application label and is not in the application at INSTALLED_APPS.

This help to solve:

on the /models.py blog:

 class BlogCategory(models.Model): some vars & methods class Meta: app_label = 'BlogCategory' 

It works 100%.

+4
Jan 11 '16 at 2:54 on
source share

Sometimes invalid .pyc files that do not match the current source code caused this problem.

I deleted all .pyc to update all of them using this bash

find . -name "*.pyc" -exec rm -rf {} \;

+2
Jan 13 '17 at 15:40
source share

The easiest and easiest way to solve this is to place the “import signals” command in the BOTTOM of the model file you are working with. This ensures that all models are loaded before the signals for this model are imported. You will need to do this for each model that you import (if you use receivers associated with specific models) or in the models.py file for the application that comes at the end of the “installed applications” in your settings.

Other solutions are only necessary if you are dealing with modeled signals where models will never be imported in the first place.

Why this is not noted in the documents is a secret, because I just caught it until I remembered that you should do it.

models.py

 from django.db import models class MyModel(models.Model): myfield1 = models.CharField() myfield2 = models.CharField() import signals 

And then in signal.py:

 from django.db.models.signals import pre_save # Or whatever you are using from django.dispatch import receiver from .models import MyModel @receiver(pre_save, sender=MyModel) def my_receiver(sender, instance, **kwargs): mysender = sender print mysender 

Like this.

+1
Feb 29 '16 at 23:23
source share

I did not get any errors in django 1.7. While migrating to django 1.8, I got this error. The reason was because the signal was defined in app/signal_receiver.py .

I created apps.py

 from django.apps import AppConfig class TasksConfig(AppConfig): name = 'core' verbose_name = "core" def ready(self): import core.signal.handler 

I moved the signal receiver to handler.py inside the signal packet.

0
Jan 22 '16 at 9:29
source share



All Articles