RemovedInDjango19Warning:Model没有声明一个明确的app_label

时间:2015-07-02 08:34:09

标签: python django django-signals django-apps deprecation-warning

经历了

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版

2 个答案:

答案 0 :(得分:19)

我在运行测试时遇到过这个问题,而这只是一个改变的问题:

from .models import MyModel

from apps.myapp.models import MyModel

答案 1 :(得分:15)

您在应用配置运行之前导入 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