Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django post_save signal on parent class with multi-table inheritance

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)

If I do:

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

Will the echo_category receiver function be called?

like image 356
Lorin Hochstein Avatar asked Feb 07 '13 18:02

Lorin Hochstein


1 Answers

post_save.connect(my_handler, ParentClass)
# connect all subclasses of base content item too
for subclass in ParentClass.__subclasses__():
    post_save.connect(my_handler, subclass)

have a nice day!

like image 159
scythargon Avatar answered Sep 18 '22 23:09

scythargon