Django

Code

Ticket #3263 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: jhilyard@uga.edu Assigned to: adrian
Milestone: Component: Forms
Version: SVN Keywords: newforms ManyToManyField
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

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

newforms_models.diff (0.8 kB) - added by Jeff Hilyard <jhilyard@uga.edu> on 01/09/07 10:55:47.
Suggested patch

Change History

01/09/07 10:55:47 changed by Jeff Hilyard <jhilyard@uga.edu>

  • attachment newforms_models.diff added.

Suggested patch

01/09/07 11:02:42 changed by Jeff Hilyard <jhilyard@uga.edu>

  • 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!

01/09/07 12:06:35 changed by Jeff Hilyard <jhilyard@uga.edu>

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

01/17/07 16:12:17 changed by

  • milestone deleted.

Milestone Version 1.0 deleted

01/18/07 14:57:09 changed by SmileyChris

  • needs_better_patch set to 1.
  • stage changed from Unreviewed to Accepted.

Thanks for the initial patch, Jeff.

01/27/07 22:57:06 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(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/Change #3263 ([patch] ValueError on form.save() for form_for_model and form_for_instance with ManyToManyField)




Change Properties
Action