Code

Opened 7 years ago

Closed 7 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
Component: Forms Version: master
Severity: normal Keywords: newforms ManyToManyField
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

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@…> 7 years ago.
Suggested patch

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by Jeff Hilyard <jhilyard@…>

Suggested patch

comment:1 Changed 7 years ago by Jeff Hilyard <jhilyard@…>

  • Summary changed from ValueError on form.save() for form_for_model and form_for_instance with ManyToManyField to [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 Changed 7 years ago by Jeff Hilyard <jhilyard@…>

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

comment:3 Changed 7 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

comment:4 Changed 7 years ago by SmileyChris

  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

Thanks for the initial patch, Jeff.

comment:5 Changed 7 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.