在Django 1.9中使用信号 [英] Use signals in Django 1.9

查看:275
本文介绍了在Django 1.9中使用信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django 1.8中,我可以用我的信号进行以下操作:一切都很好:



__ init__.py:

 从.signals import * 

signals.py:

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ value_to_string(instance).split():
如果word.startswith('#'):
word = render_to_string('hashtags / _link.html',
{'hashtag':word。 lower()[1:]})

html.append(word)
instance.hashtag_enabled_text =''.join(html)
/ pre>

在Django 1.9中,我收到此错误: django.core.exceptions.AppRegistryNotReady:Apps尚未加载。



我知道它来自 __ init __。py ,但是nyone知道一个解决办法吗?我假设可能把它放在模型中?如果是这样,有人可以告诉我怎么做吗?



models.py:

 类注释(HashtagMixin,TimeStampedModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
text = models。 TextField(max_length = 240)
hashtag_enabled_text = models.TextField(blank = True)
hashtag_text_field ='text'

objects = CommentManager()

类Meta:
app_label ='comments'

def __unicode __(self):
return self.text

提前谢谢!

解决方案

发行说明


所有模型需要在已安装的应用程序中定义,或声明一个显式的app_label。此外,在加载应用程序之前不可能导入它们。特别是,不可能在应用程序的根包中导入模型。


通过在<$ c中导入您的信号$ c> __ init __。py ,您将间接导入您应用程序的根包中的模型。避免这种情况的一个选择是将发件人更改为字符串:

  @receiver(pre_save,sender ='< appname> .Comment')
def process_hashtags(sender,instance,** kwargs):
...

在1.9中连接使用 @receiver decorator的信号的推荐方法是创建一个< a href =https://docs.djangoproject.com/en/1.9/ref/applications/#application-configuration =nofollow>应用程序配置,并将信号模块导入 AppConfig.ready()


In Django 1.8, I was able to do the following with my signals, and all was well:

__init__.py:

from .signals import *

signals.py:

@receiver(pre_save, sender=Comment)
def process_hashtags(sender, instance, **kwargs):
    html = []
    for word in instance.hashtag_field.value_to_string(instance).split():
        if word.startswith('#'):
            word = render_to_string('hashtags/_link.html',
                                    {'hashtag': word.lower()[1:]})

        html.append(word)
        instance.hashtag_enabled_text = ' '.join(html)

In Django 1.9, I get this error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I know it's coming from the __init__.py, but does anyone know a workaround for this? I'm assuming maybe putting it in the models? If so, could someone please show me how to do that?

models.py:

class Comment(HashtagMixin, TimeStampedModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    text = models.TextField(max_length=240)
    hashtag_enabled_text = models.TextField(blank=True)
    hashtag_text_field = 'text'

    objects = CommentManager()

    class Meta:
        app_label = 'comments'

    def __unicode__(self):
        return self.text

Thank you in advance!

解决方案

From the release notes:

All models need to be defined inside an installed application or declare an explicit app_label. Furthermore, it isn’t possible to import them before their application is loaded. In particular, it isn’t possible to import models inside the root package of an application.

By importing your signals in __init__.py, you're indirectly importing your models in the root package of your application. One option to avoid this is to change the sender to a string:

@receiver(pre_save, sender='<appname>.Comment')
def process_hashtags(sender, instance, **kwargs):
    ...

The recommended way to connect signals that use @receiver decorator in 1.9 is to create an application configuration, and import the signals module in AppConfig.ready().

这篇关于在Django 1.9中使用信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆