Expensive django calculation needs to run on model change

I am working on a django application that has a model with a field that needs to be calculated when the model changes. The calculation itself is relatively expensive (1+ seconds) especially if/when this field is recalculated for a large queryset. It is important that this field is recalculated immediately following the model change because it determines the workflow which the user needs to follow and the field value is propagated to the UI for the user to see. At a minimum the calculation needs to be guaranteed to run after update or some way to indicate the calculation is stale.

The current implementation which was not an issue because the calculation was previously less complicated/expensive (before requested changes by the client) is as follows:

  • user makes edit in UI and model is updated
  • API receives changes and applies changes to model
  • model .save() is overridden so the field is calculated and set

With this implementation we also disabled the queryset manager on the model to prevent the use of .update() to ensure that field is always recalculated on .save().

I am curious on how others would implement something like this to be as efficient as possible and not relying on the blocking save to finish.

Вернуться на верх