Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Process Related Object when Saving
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
ghachey  
View profile  
 More options May 16 2012, 12:55 am
From: ghachey <ghac...@gmail.com>
Date: Tue, 15 May 2012 21:55:14 -0700 (PDT)
Local: Wed, May 16 2012 12:55 am
Subject: Process Related Object when Saving
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ghachey  
View profile  
 More options May 16 2012, 1:15 am
From: ghachey <ghac...@gmail.com>
Date: Tue, 15 May 2012 22:15:01 -0700 (PDT)
Local: Wed, May 16 2012 1:15 am
Subject: Re: Process Related Object when Saving
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

On May 16, 3:55 pm, ghachey <ghac...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Aymerich  
View profile  
 More options May 17 2012, 6:10 pm
From: Marc Aymerich <glicer...@gmail.com>
Date: Fri, 18 May 2012 00:10:10 +0200
Local: Thurs, May 17 2012 6:10 pm
Subject: Re: Process Related Object when Saving

An alternative could be register to the post_save signal of the EntryLog model.

--
Marc

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »