Django post_save() signal implementation Django post_save() signal implementation python python

Django post_save() signal implementation


If you really want to use signals to achieve this, here's briefly how,

from django.db.models.signals import post_savefrom django.dispatch import receiverclass TransactionDetail(models.Model):    product = models.ForeignKey(Product)# method for updating@receiver(post_save, sender=TransactionDetail, dispatch_uid="update_stock_count")def update_stock(sender, instance, **kwargs):    instance.product.stock -= instance.amount    instance.product.save()


Personally I would override the TransactionDetail's save() method and in there save the new TransactionDetail and then run

self.product.stock -= self.amountself.product.save()


If you want to avoid getting maximum recursion depth exceeded, then you should disconnect signals, before saving within the signal handler. The example above (Kenny Shen's answer), would then be:

from django.db.models.signals import post_savefrom django.dispatch import receiverclass TransactionDetail(models.Model):    # ... fields here# method for updating@receiver(post_save, sender=TransactionDetail, dispatch_uid="update_stock_count")def update_stock(sender, instance, **kwargs): instance.product.stock -= instance.amount post_save.disconnect(update_stock, sender=TransactionDetail) instance.product.save() post_save.connect(update_stock, sender=TransactionDetail)

This is described thoroughly in Disconnect signals for models and reconnect in django, with a more abstract and useful example.

Also see: https://docs.djangoproject.com/en/2.0/topics/signals/#disconnecting-signals in the django docs.