Have you considered using a cache/store backend like redis[1] instead of saving the result to the database?
Using this approach, you can write a custom model method for returning the calculation, which can be called manually every time you need the result. The key difference being that this method try to get the result from the cache first, and defer the calculation to the cases when that fails. Finally, add a method for invalidating the cache (i.e. deleting the value that was previously stored) and call that you can every time either one of the models gets saved. Write a key composed of the model's app_label, module_name and pk, a custom string to identify this calculation, perhaps some params and you're good to go. django-cache-utils[2] can help in some cases, or you can write the cache setting/getting/invalidating on your own. I (and many others like me) recommend redis and django-redis-cache[3] for this. Cheers, AT [1] http://redis.io/ [2] https://bitbucket.org/kmike/django-view-cache-utils [3] https://github.com/sebleier/django-redis-cache On Mon, Sep 19, 2011 at 12:24 PM, graeme <[email protected]> wrote: > I want to save a calculated value to a model field. The calculation > takes a few seconds and I need to query by it, so I think the > denormalisation is justified. > > The problem is that the calculation requires data from two related > models: one of which is the "through" model of a ManyToMany > relationship, the other of which has a for (i.e. we are looking at the > reverse side of it). > > Is there any way of calling the calculation after BOTH the related > models have been saved? If there is a way of guaranteeing the order in > which they are saved i could use a post_save or m2m_changed signal on > one of them. > > Alternatively, as both are inlines in the admin, I think I could > customise save_formset, but I have not been able to find any examples > or much documentation for this. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

