Thursday, August 25, 2011

Update the parent model when a related (inline) model changed?

Dear Django list,

what is the best way to automatically update a parent model when one of
it's related models (i.e. the "inline" models in the admin interface)
changed?

Overall, we use a main model ("Employee") and several related models,
one of which is a cache with monthly sums. The main Employee model has a
field "monthly-sums-cache is valid before...", and we would like to
intelligently update this field, whenever the Employee model itself was
changed, or one of its related models.
For example, when someone corrected the date of: worked hours, taken
holidays, changes in contract, etc., we'd need to learn the change (the
date of the chance) and update the related valid-before field in the
Employee model accordingly.

(We'm primarily looking for a solution that works in the Django admin
interface, but if it works everywhere, i.e. independently from the admin
interface, that would be even better.)

I've looked into the ModelAdmin.save_model() method documented at
<https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#modeladmin-methods>,
but it seems to deal only with the parent model.

By the way, a question about save_model():
How do I learn the old and new values of the fields in save_model(),
i.e. how can I know which fields have changed?

Alternatively, overriding Model.save() might be an option, but is it
safe to manipulate *and* save a *related* model instance from
Model.save()? In Model.save(), how can we learn if a field has changed,
rather than see only the new values?


I'd be very grateful for your advice!

Many thanks and best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate