RemovedInDjango19Warning: Model doesn't declare an explicit app_label

大城市里の小女人 提交于 2019-11-30 03:20:34

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/31179459/removedindjango19warning-model-doesnt-declare-an-explicit-app-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!