.save_m2m() failing with multi-database error

246 views
Skip to first unread message

Ben Standefer

unread,
Sep 6, 2011, 3:26:37 AM9/6/11
to django-d...@googlegroups.com
I think I might have found an issue related to https://code.djangoproject.com/ticket/14849.  The last comment says to please email the django-dev group if there are any questions about the ticket, so here I am.  I've done my best to provide a complete test case.

I have an Event model with property 'times', which is a ManyToManyField to an EventTime model.  https://gist.github.com/c7d8319df96f5eb22cf3
I am processing data from a POST request with a ModelForm on Event, with a custom field and widget: https://gist.github.com/653adb9b0ecbcc823339  The widget takes a few POST fields and generates a set of date ranges as a list of tuples [(<start>, <end>), (<start>, <end>)...].  The to_python() method of the custom field takes the date ranges and returns a list of EventTime objects.

When I try to .save_m2m() the form (test case here: https://gist.github.com/6da4f69ade9bc7dc5b3c) I get...
ValueError: Cannot add "<EventTime: EventTime object>": instance is on database "default", value is on database "None"

This looks it might be an issue with the new multi-database stuff.  I could also be misunderstanding something, but after scouring the documentation for several hours I don't see anything implying that this wouldn't work.  I suspect the unusual thing I'm doing is creating instances of a Django model within my field's to_python() method.  Most of the existing fields use to_python() to cast to datetime or Decimal.  Is it not possible to convert value(s) into instances of Django models?

Thanks for your help.

-Ben Standefer

-Ben

Greg Turner

unread,
Sep 25, 2011, 9:15:49 PM9/25/11
to django-d...@googlegroups.com
FWIW Ben, I had the same error, but under different circumstances
(your message below is the top google result for the error!). For me,
the problem was caused by a custom save() method in the model not
calling super().save(). Did you omit such a method in your gist.github
models?

> --
> You received this message because you are subscribed to the Google Groups "Django developers" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/django-developers/-/uVcisHlDAzkJ.
> To post to this group, send email to django-d...@googlegroups.com.
> To unsubscribe from this group, send email to django-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.

--

Dr Greg Turner
Director, Technical Information Architect
the Interaction Consortium
http://interaction.net.au
Phone: +61 2 8060 1067
skype: gregturner
Follow us on twitter:
@theixc
@gsta
Be green! Read from the screen.

Reply all
Reply to author
Forward
0 new messages