django暂时禁用信号 [英] django temporarily disable signals

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

问题描述

我在django中有一个信号回调:

I have a signal callback in django:

@receiver(post_save, sender=MediumCategory)
def update_category_descendants(sender, **kwargs):

    def children_for(category):
        return MediumCategory.objects.filter(parent=category)

    def do_update_descendants(category):
        children = children_for(category)
        descendants = list() + list(children)

        for descendants_part in [do_update_descendants(child) for child in children]:
            descendants += descendants_part

        category.descendants.clear()
        for descendant in descendants:
            if category and not (descendant in category.descendants.all()):
                category.descendants.add(descendant)
                category.save()
        return list(descendants)

    # call it for update
    do_update_descendants(None)

但在func我正在使用 .save()在模型 MediumCategory 上,它再次发出信号。如何禁用它?完美的解决方案将是一个,其中包含语句,其中包含一些魔术。

but in the function body I'm using .save() on models MediumCategory that couses that the signal is dispatched again. How can I disable it; the perfect solution would be a with statement with some 'magic' inside.

更新: strong>
这是最终解决方案,如果有兴趣的话。

UPDATE: That's final solution, if anyone interested.

class MediumCategory(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(blank=True)
    parent = models.ForeignKey('self', blank=True, null=True)
    parameters = models.ManyToManyField(AdvertisementDescriptonParameter, blank=True)
    count_mediums = models.PositiveIntegerField(default=0)
    count_ads = models.PositiveIntegerField(default=0)

    descendants = models.ManyToManyField('self', blank=True, null=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(MediumCategory, self).save(*args, **kwargs)

    def __unicode__(self):
        return unicode(self.name)
(...)
@receiver(post_save, sender=MediumCategory)
def update_category_descendants(sender=None, **kwargs):
    def children_for(category):
        return MediumCategory.objects.filter(parent=category)

    def do_update_descendants(category):
        children = children_for(category)
        descendants = list() + list(children)

        for descendants_part in [do_update_descendants(child) for child in children]:
            descendants += descendants_part

        if category:
            category.descendants.clear()
            for descendant in descendants:
                category.descendants.add(descendant)
        return list(descendants)

    # call it for update
    do_update_descendants(None)


推荐答案

也许我错了,但我认为 category.save()不需要你的代码,add()是足够的,因为更改是在后代,但在类别。

Perhaps I'm wrong, but I think that category.save() is not needed in your code, add() is enough because change is made in descendant but in category.

另外,为了避免信号,您可以:

Also, to avoid signals you can:


  • 断开信号并重新连接。

  • 使用更新 Descendant.objects.filter(pk = descendant.pk).update(category = category)

  • Disconnect signal and reconnect.
  • Use update: Descendant.objects.filter( pk = descendant.pk ).update( category = category )

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

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