Opened 18 years ago

Closed 18 years ago

#3263 closed defect (fixed)

[patch] ValueError on form.save() for form_for_model and form_for_instance with ManyToManyField

Reported by: jhilyard@… Owned by: Adrian Holovaty
Component: Forms Version: dev
Severity: normal Keywords: newforms ManyToManyField
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The database API wants a list of related object instances, not the list of primary key values supplied by clean_data (actually integers as unicode strings) when you .save() a form_for_model or form_for_instance instance.

ValueError at /edit/news/2/
objects to add() must be Audience instances
Request Method: POST
Request URL: [my url]
Exception Type: ValueError
Exception Value: objects to add() must be Audience instances
Exception Location: /usr/lib/python2.4/site-packages/django/db/models/fields/related.py in _add_items, line 327

here are the relevant bits of the models:

class News(models.Model):

audiences = models.ManyToManyField(Audience,filter_interface=models.HORIZONTAL)

class Audience(models.Model):

name = models.CharField(maxlength=64,unique=True,db_index=True)

Attachments (1)

newforms_models.diff (768 bytes ) - added by Jeff Hilyard <jhilyard@…> 18 years ago.
Suggested patch

Download all attachments as: .zip

Change History (6)

by Jeff Hilyard <jhilyard@…>, 18 years ago

Attachment: newforms_models.diff added

Suggested patch

comment:1 by Jeff Hilyard <jhilyard@…>, 18 years ago

Summary: ValueError on form.save() for form_for_model and form_for_instance with ManyToManyField[patch] ValueError on form.save() for form_for_model and form_for_instance with ManyToManyField

This is a suggestion that works for me since I am an impatient person (and I have Deadlines!).

I have only tested it in the case where the form_for_model or _instance is invoked on the model that contains the ManyToManyField, so I can't comment on whether it works in the backwards relation case.

I love the work you guys are doing and can't wait for 1.0!

comment:2 by Jeff Hilyard <jhilyard@…>, 18 years ago

I should have used "_default_manager" instead of "objects" in the new line 41 of the attached .diff

comment:3 by (none), 18 years ago

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:4 by Chris Beaven, 18 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Thanks for the initial patch, Jeff.

comment:5 by Adrian Holovaty, 18 years ago

Resolution: fixed
Status: newclosed

(In [4439]) Fixed #3263 -- newforms form_for_model() and form_for_instance() now handle foreign-key and many-to-many data properly. Thanks for the patch, Jeff Hilyard

Note: See TracTickets for help on using tickets.
Back to Top