m2m assignments lost when saving in admin but not shell

27 views
Skip to first unread message

Shawn Milochik

unread,
Aug 17, 2011, 11:44:04 PM8/17/11
to django...@googlegroups.com
I have some code that modifies related items when a model is saved. I've
tried this by both using a post_save signal and by putting the code
directly in a save() override.

When I save an instance in the Django admin, it never works.
When I save an instance in ./manage.py shell it always works.

Why would this be?

For instance, if my code (in the save override) is this:

self.some_m2m_field.clear()
self.some_m2m_field.add(this_thing)

Then if I go into the admin and save the instance, this_thing is not
attached to the instance. But if I do a .save() in the shell and check
it has been assigned.

I assume I'm missing something fundamental about this.

Thanks,
Shawn

Matt Schinckel

unread,
Aug 18, 2011, 12:04:01 AM8/18/11
to django...@googlegroups.com
I had similar problems, but I put it down to doing something 'unusual'.

Try setting a pdb breakpoint in your admin class, and see if there is anything odd. I found that I was getting failures due to save(commit=False) meaning that an object had no primary key, and I had to do some fancy stuff to replace the save_m2m method with one that handled my relationship.

Matt.

Tom Evans

unread,
Aug 18, 2011, 4:48:30 AM8/18/11
to django...@googlegroups.com

I've never encountered this Shawn, but the primary difference between
those two is that saving in the admin will go through a ModelForm.
Does the admin site use frm.save(commit=False) and then run
frm.save_m2m()? frm.save_m2m() would definitely be run after both an
overwritten save() and a post_save hook.

Worth a thought.

Cheers

Tom

Andre Terra

unread,
Aug 18, 2011, 9:24:24 AM8/18/11
to django...@googlegroups.com
Your instance is probably not yet saved to the database, so the m2m-related objects don't know which instance to connect to. There's problem something wrong in your save() method. I often use "assert False, some_var" to check if at some point the variable is already set or not.

And I get the same problem as you so many times that forms have become the thing I hate the most about Django, even though my lack of knowledge is the one to be blamed, not the framework.


Cheers,
AT


--
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.


Shawn Milochik

unread,
Aug 18, 2011, 9:54:41 AM8/18/11
to django...@googlegroups.com
On 08/18/2011 09:24 AM, Andre Terra wrote:
> Your instance is probably not yet saved to the database, so the
> m2m-related objects don't know which instance to connect to. There's
> problem something wrong in your save() method. I often use "assert
> False, some_var" to check if at some point the variable is already set
> or not.
>

This is definitely not the case, because these are always records that
already exist, if I'm editing them in the admin.

Shawn Milochik

unread,
Aug 24, 2011, 10:57:58 AM8/24/11
to django...@googlegroups.com
In case anyone's interested:

I still don't know what the problem was, but I solved it by just doing a
delay call to a Celery task, so it happens asynchronously.

I was already using Celery/RabbitMQ, and the functionality was one of
the things I wanted to make async anyway.

Reply all
Reply to author
Forward
0 new messages