具有多表继承的父类上的Django post_save信号 [英] Django post_save signal on parent class with multi-table inheritance

查看:74
本文介绍了具有多表继承的父类上的Django post_save信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,如果您有使用多表继承的模型,并且在父类上为post_save信号定义了接收器,那么在保存子类的实例时会调用该接收器函数吗?

In Django, if you have models that use multi-table inheritance, and you define a receiver for a post_save signal on the parent class, does that receiver function get called when an instance of the child class is saved?

从另一个问题中借用示例

Borrowing an example from another question:

class Animal(models.Model):
    category = models.CharField(max_length=20)

class Dog(Animal):
    color = models.CharField(max_length=10)

def echo_category(sender, **kwargs):
    print "category: '%s'" % kwargs['instance'].category

post_save.connect(echo_category, sender=Animal)

如果我这样做:

>>> dog = Dog.objects.get(...)
>>> dog.category = "canine"
>>> dog.save()

echo_category

推荐答案

签出:
https://code.djangoproject.com/ticket/9318
看来,大多数信号会在子类中传播给超类。

Check out: https://code.djangoproject.com/ticket/9318 It appears that most propagate the signal to the super in the subclass.

这篇关于具有多表继承的父类上的Django post_save信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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