RemovedInDjango19Warning: Model doesn't declare an explicit app_label
Asked Answered
D

2

10

Have gone through

Django 1.9 deprecation warnings app_label

but answers couldn't fix my problem, so asking again.

I have an app that is added to INSTALLED_APPS in settings.

when ever I run manage.py runserver, I get this warning,

[trimmed path to project]/catalog/models.py:9: RemovedInDjango19Warning: Model class catalog.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):

The code from my app,

signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from models import Category

@receiver(post_save, sender=Category)
def someSignal(sender, **kwargs):
    pass

apps.py

from django.apps import AppConfig

class CatalogConfig(AppConfig):
    name = 'catalog'
    verbose_name = 'Catalogue'

init.py

import signals

default_app_config = 'catalog.apps.WhosConfig'

Django version 1.8.2 on Python 2.7.8

Divorce answered 2/7, 2015 at 8:34 Comment(1)
"was imported before its application was loaded": Your __init__.py imports signals.py which imports models.py. The latter thus happens before django registers the app, which is now deprecated. If you want the signals in a separate app, try to import signals.py from models.py or similar.Geraldo
O
15

You are importing models.py before app configuration run.

To fix it, you could import and configure signals in CatalogConfig.ready method.

like this:

signals.py

def someSignal(sender, **kwargs):
    pass

apps.py

from django.apps import AppConfig
from django.db.models.signals import post_save


class CatalogConfig(AppConfig):
    name = 'catalog'
    verbose_name = 'Catalogue'

    def ready(self):
        from .signals import someSignal
        post_save.connect(
            receiver=someSignal,
            sender=self.get_model('Category')
        )

you may want to check ready method in documentation

Owlet answered 2/7, 2015 at 9:4 Comment(0)
T
20

I experienced this issue when running tests and it was simply a matter of changing:

from .models import MyModel

to

from apps.myapp.models import MyModel
Tatting answered 9/9, 2016 at 22:22 Comment(3)
This fixed my problem. but I don't get it. Why does the first one not work?Spindle
@Spindle probably because the framework was getting confused using a relative path. Instead, using an absolute path guarantees the validity of the path. This shouldn't be an issue, though.Photographic
for me I had to do from myapp.models import MyModel so it may depend on where you're executing tests fromRunstadler
O
15

You are importing models.py before app configuration run.

To fix it, you could import and configure signals in CatalogConfig.ready method.

like this:

signals.py

def someSignal(sender, **kwargs):
    pass

apps.py

from django.apps import AppConfig
from django.db.models.signals import post_save


class CatalogConfig(AppConfig):
    name = 'catalog'
    verbose_name = 'Catalogue'

    def ready(self):
        from .signals import someSignal
        post_save.connect(
            receiver=someSignal,
            sender=self.get_model('Category')
        )

you may want to check ready method in documentation

Owlet answered 2/7, 2015 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.