Process Related Object when Saving

106 views
Skip to first unread message

ghachey

unread,
May 16, 2012, 12:55:14 AM5/16/12
to Django users
Hi;

I am trying to generate a printable report when saving objects for the
first time. This is easy enough except I can't easily access related
objects by overriding save() as they are not saved yet, nor are they
available within the save() method.

The new ModelAdmin.save_related() in Django 1.4 method seems to have
been created for just my need. The docs say: "Here you can do any pre-
or post-save operations for objects related to the parent. Note that
at this point the parent object and its form have already been saved."
From what I read this seems perfect for my use case.

I tried overriding the method as follows:

def save_related(self, request, form, formsets, change):

if not change:
# Generate and send report only if creating for first time
parent_obj = form.instance
import pdb; pdb.set_trace();

1) At that point when I look into the database I don't see the saved
parent object? This seems to contradict the docs.
2) I can access cleaned data from the formsets, but is there an easy
way to simply get the related instances as I would with
parent.child_set.all(). This would make my life much easier as I have
written the code already to print a LaTeX report from any Django model
(by looping _meta.fields approach).

I would appreciate any help.

--
GH

ghachey

unread,
May 16, 2012, 1:15:01 AM5/16/12
to Django users
Hi again;

Looking at the source code I realised that form.save_m2m() must be
called.

def save_related(self, request, form, formsets, change):

form.save_m2m()
for formset in formsets:
self.save_formset(request, form, formset, change=change)

if not change:
# Generate and send report only if creating for first time
(insert)
parent_obj = form.instance
generate_pdf(parent_obj)

I think the above does what I wanted; so far so good anyway.

--
GH

Marc Aymerich

unread,
May 17, 2012, 6:10:10 PM5/17/12
to django...@googlegroups.com
On Wed, May 16, 2012 at 7:15 AM, ghachey <gha...@gmail.com> wrote:
> Hi again;
>
> Looking at the source code I realised that form.save_m2m() must be
> called.
>
> def save_related(self, request, form, formsets, change):
>
>        form.save_m2m()
>        for formset in formsets:
>            self.save_formset(request, form, formset, change=change)
>
>        if not change:
>            # Generate and send report only if creating for first time
> (insert)
>            parent_obj = form.instance
>            generate_pdf(parent_obj)
>
> I think the above does what I wanted; so far so good anyway.

An alternative could be register to the post_save signal of the EntryLog model.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>



--
Marc
Reply all
Reply to author
Forward
0 new messages