编程语言
首页 > 编程语言> > python – RemovedInDjango19Warning:Model没有声明一个显式的app_label

python – RemovedInDjango19Warning:Model没有声明一个显式的app_label

作者:互联网

经历过

Django 1.9 deprecation warnings app_label

但答案无法解决我的问题,所以再问一遍.

我有一个应用程序添加到设置中的INSTALLED_APPS.

当我运行manage.py runserver时,我收到此警告,

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

我的应用程序的代码,

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'

关于Python 2.7.8的Django 1.8.2版

解决方法:

您正在应用配置运行之前导入models.py.

要修复它,您可以在CatalogConfig.ready方法中导入和配置信号.

像这样:

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')
        )

您可能想要检查就绪方法in documentation

标签:django-signals,django-apps,python,django,deprecation-warning
来源: https://codeday.me/bug/20191006/1861635.html