Hello,

In brief, does anyone know of a way to hook code into the save in the
admin so that it executes after the main model and any relations
created because of inlines are committed to the database?  I don't
necessarily need it to give me a re-loaded model instance, but I would
at least like to be able to get at a model instance that reflects the
state of the database post-save so I can update fields in the parent
model based on the current state of its relations.

I am using the django admin code from the trunk version of django (1.3
pre-alpha SVN-13861).

In my models, I have an event model that can be connected to a number
of different related objects:
- event dates (which hold start and end date, start and end time, and
notes particular to each performance, such as understudies performing)
- event presenters (a ManyToMany relationship)
- event series (a ManyToMany relationship)

After adding or removing dates, presenters, or series, I would like to
be able to call a method that looks at all the related objects for an
event and places values in a few fields on the event:
- figures out the start and end date for the event based on all event
dates and places them in datetime fields in the event model.
- creates a string of all the presenter names, separated by semi-
colons, and places that in a string field.
- creates a string of all the series names, separated by semi-colons,
and places that in a string field.

I am trying to get information from these relationships back into the
base model so they can be used in the admin, for searching and
sorting.

Here is what I tried so far:
- I tried the save_model() hook in the admin class for event, but the
relationships have not yet been added to the event at this point (or
to the database - I tried querying the database to pull in all of the
event_date table rows for the event's ID, for example, and nothing was
returned).
- I tried overriding the save() method on the event so it rebuilds the
cached fields directly from the database.  Since this is at the same
point in the admin save process as save_model(), this also did not
work.
- I tried making my own join model for event to presenter (as a test),
then overrode its save() method to rebuild its associated event's
cached presenter string on update.  No dice here either.
- I tried reloading the event using it's ID at each of these points,
to see if the database had been updated but the model instance needed
to be reloaded to reflect changes.  This didn't work in any of the
three places above, indicating to me at least that the updates to the
main model and inline models are combined into a transaction that is
only committed after everything is validated and there are no errors.

So it looks like all the updates to the main model and inline models
are combined into a transaction, and there is no easy way to insert
code after that transaction is committed.

Does anyone know of a way to hook code into the save in the admin so
that it executes after the main model and any relations created
because of inlines are at the least committed to the database?  I
don't necessarily need it to give me a re-loaded model instance, but I
would at least like to be able to get at a model instance that
reflects the state of the database post-save.

I am hoping I am missing something basic, but I've searched the
internet, this group, and the django issue-tracking system and have
not found anything.  I'll appreciate any help anyone can provide.

Thanks,

Jonathan Morgan

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

Reply via email to