0

I have two models, Order and OrderRow (1:n). What I would like is when something changes on the OrderRow (added, removed, price change, etc.) that that "effect" is calculated to the parent Order. In this case, sum the total of all the OrderRow in Order.

The OrderRow (a admin.TabularInline subclass) is setup as inlines on Order.

I did implement the django.db.models.signals.post_save but the problem here is that the Order post_save is fired before the OrderRow updates.

Is there any nice way (besides implementing a db trigger) for updating this the 'django-way' ?

All this under Django Admin

Roger
  • 7,535
  • 5
  • 41
  • 63
  • You can attach `post_save` to OrderRow instead of Order. Whenever an order row updates, get all order rows like `order_row.order.orderrow_set.all`, perform calculations and update order accordingly. – Nafees Anwar Sep 14 '19 at 10:33
  • @NafeesAnwar thanks, I understand that. But it's very inefficient. The problem is when you create an order with 10 rows, it gets fired 10x. Not truly elegant – Roger Sep 14 '19 at 10:40

0 Answers0