Cannot use the save() correctly

595 views
Skip to first unread message

william

unread,
Jul 8, 2006, 1:49:29 PM7/8/06
to Django users
I've been trough the save() as documented here:
http://code.djangoproject.com/wiki/RemovingTheMagic#Overridingsaveanddeletemodelmethods

But, this does not work for fields having a ManyToMany relation.
If you take the example with "publication" and "article" as described
here: http://www.djangoproject.com/documentation/models/many_to_many/

If you put the following into the "Article" class:
def save(self):
print "Before",self.publications.all()
super(Article, self).save()
print "after",self.publications.all()

You'll see that you'll always see the same result before and after the
save.
(standard fields are yet working correctly)
It seems that the manytomany related data are saved after the complete
execution of save().
I'm using revision 3275
In my case this is annoying.

Any ideas for a pre-save and post-save for all data (even manytomany)?


Thanks

william

unread,
Jul 8, 2006, 3:58:08 PM7/8/06
to Django users

I've just tried with the dispatcher... same result.

Is there any caching mechanism with the database link ?

Malcolm Tredinnick

unread,
Jul 8, 2006, 9:44:52 PM7/8/06
to django...@googlegroups.com

Related items (the things being saved into the many-to-many relation)
are not saved as part of a model's save method, as you have discovered.
Instead, the Add- and ChangeManipulators save the many-to-many items
later. In fact, for adding a new item, this is basically required,
because you need to know the new instance's primary key value before you
can save a reference to it in the m2m join table -- and that value does
not necessarily exist before it is saved to the database.

At the moment, any workaround is going to involve custom manipulators, I
suspect (although I may be missing something obvious). We might be able
to come up with something a bit nicer with the upcoming manipulator
refactoring that is on the table.

This does come up from time to time when somebody wants to take action
based on a new relationship being created. So you're not on the fringe
here by wanting this.

Sorry, not much encouragement there except to say it's a known problem
(well, at least, I consider it a "problem"). Maybe somebody smarter than
me can suggest an easy solution at the moment *shrug*.

Regards,
Malcolm

william

unread,
Jul 9, 2006, 3:26:35 AM7/9/06
to Django users

Thanks Malcome,

I was thinking of a kind of dispatch.connect "related-pre-save" and
"related-post-save" for the manytomany related data.
A bit like the existing "pre-save" and "post-save".

The idea to look at the manipulator is not bad too.
I'll investigate it, thanks.

lvs

unread,
Jun 12, 2009, 5:17:57 AM6/12/09
to william, django...@googlegroups.com

On Jul 9 2006, 3:26 pm, "william" <will...@opensource4you.com> wrote:
> Thanks Malcome,
>
> I was thinking of a kind of dispatch.connect "related-pre-save" and
> "related-post-save" for the manytomany related data.
> A bit like the existing "pre-save" and "post-save".

please see the ticket
http://code.djangoproject.com/ticket/5390
but the ManyRelatedManager signals will not be added before 1.2 :-(


>
> The idea to look at the manipulator is not bad too.
> I'll investigate it, thanks.
>
> Malcolm Tredinnick wrote:
> > On Sat, 2006-07-08 at 17:49 +0000, william wrote:
> > > I've been trough the save() as documented here:

> > >http://code.djangoproject.com/wiki/RemovingTheMagic#Overridingsaveand...

Reply all
Reply to author
Forward
0 new messages