Tuesday, August 30, 2011

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

Hi all,

many thanks to everyone who replied!

For the records/archives, here is the solution that we eventually
implemented:

Am 25.08.2011 16:19, schrieb Carsten Fuchs:
> 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?

For the related models (except for the single related model that
contains the cache (the dependent monthly sums)), overriding the save()
and delete() methods was very simple and straightforward:
The code in these function updates the "cache is valid before" member in
the main model before running the save() or delete() methods in the
super class.

For the main model itself, it turned out that overriding delete() was
never necessary, because if the main model is deleted, it's cached data
is deleted as well, no need to update the "cache is valid before" member.

Overriding save() in the main model (for cases when a change to the main
model requires an update of the cache) turned out to be not a good idea,
because it easily conflicts with the newly overridden save() methods of
the related models, whose implementation in turn calls save() of the
main model.
Instead, for the main model, we overrode save_model() in the ModelAdmin.

In summary, overriding save() for the related models and save_model() in
ModelAdmin for the main model solved the problem.

Best regards,
Carsten

--
Cafu - the open-source Game and Graphics Engine
for multiplayer, cross-platform, real-time 3D Action
Learn more at http://www.cafu.de

--
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.

No comments:

Post a Comment