1

I need to trigger any changes of some models. My models are: Brand, Product, Package. Package has fk to Product and Product has fk to Brand. So when some instances of these models are changed or created or deleted, I need to send signal. Can I implement it with post_save signal? I thought that if I write post_save signal for lower model: Package, then any changes with Brand or Product will be triggered. But it's not

Andrew
  • 423
  • 1
  • 6
  • 16

1 Answers1

0

Why your signals are not triggered? Django's post_save is triggered in the end of model's save() method (docs). When you are updating your Package, Product still contains only a key to the Package model. So Product is not calling save().

What you can do:

  1. Create several post_save signals.
  2. Override models' save() method.

To bind several post_save signals you simply do:

post_save.connect(do_package_stuff, Package, weak=False, dispatch_uid='package_post_save')
post_save.connect(do_product_stuff, Product, weak=False, dispatch_uid='product_post_save')
post_save.connect(do_brand_stuff, Brand, weak=False, dispatch_uid='brand_post_save')

Overriding save() is easier (and I can say more recommeneded). You can see this question if you are instrested in what is better.

Community
  • 1
  • 1
sobolevn
  • 16,714
  • 6
  • 62
  • 60