Wagtail models and pages are just Django models.
Are all children (and grand children) of the same model? In that case you could just register a post_save signal on the child model, but in general, I'd recommend using post_save only for non-mutating actions like sending an email when an object changes, unless you take the following things into consideration:
- This kind of processing can become very slow very quickly. If you have multiple quotations, you'll need to use an atomic transaction, otherwise you'll make a new db connection when saving every quotation.
- You'll also need to prefetch children and grandchildren to prevent multiple database calls.
- When saving other models inside a post_save you run the risk of creating an endless loop of post_save signals.
I think a better way would be to add a @property called price
on Quotation instead of storing the price in the database:
class Quotation(models.Model):
# Model fields here
...
@property
def price(self):
return sum([child.price for child in self.children])
This will only calculate a price when you call quotation.price
. You could speed up this calculation by prefetching children when receiving Quotations.