Tuesday, November 20, 2012

Re: How to implement pre-save operations for inlines in the admin interface?

Just for info, to complete this:

I read in the admin interface source code (contrib/admin/options.py) and
found a solution myself. It's not as clear and straightforward as one
would wish, but simple and robust:

We now simply override the `ModelAdmin.save_formset()` function like this:


def save_formset(self, request, form, formset, change):
if formset.model == Period:
# Save normally.
instances = formset.save()

if instances:
# At least *one* period has changed and was saved,
# so check/update *all* periods of that staff member.
instances[0].staff.UpdateAllPeriods()
else:
# The default implementation of save_formset():
formset.save()


(The ugly trick is the `instances[0].staff` to obtain the parent object.)

Function `UpdateAllPeriods()` simply loops over the `period_set` of the
Staff instance and updates the periods as required.

:-)

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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate