django - Signal for M2M changed on a through field -
before switching m2m field through field had signal call methods on instance update of values. switched 'through' type m2m , stopped functioning. have work around calling functions in serializer, kind of nasty. how fix it?
signal
@receiver(m2m_changed, sender=order.items.through) def update_order_when_items_changed(sender, instance, **kwargs): instance.set_weight() instance.set_total_price() instance.save()
current models
class orderitem(models.model): order = models.foreignkey('main.order') item = models.foreignkey('main.item') quantity = models.integerfield(default=1) class order(models.model): user = models.foreignkey("main.shopuser") items = models.manytomanyfield("main.item", through='main.orderitem') placed = models.booleanfield(default=false) date_placed = models.datetimefield(null=true, blank=true)
i feel silly not figuring out before. instead of m2m_changed signal use post_save
, post_delete
on through model , should same thing. in serializer remove instance.save()
if not doing other changes.
@receiver((post_save, post_delete), sender="main.orderitem") def update_order_when_items_changed(sender, instance, **kwargs): instance.order.set_weight() instance.order.set_total_price() instance.order.save()
Comments
Post a Comment