Opened 6 years ago

Closed 6 years ago

#6022 closed (invalid)

m2m ValueError with form_for_instance on fresh instance

Reported by: sjulean Owned by: nobody
Component: Forms Version: newforms-admin
Severity: Keywords: m2m form_for_instance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Assume MyModel is a model with an m2m field. I tried this:

my_instance = MyModel(some_fk=some_value)
MyModelForm = forms.form_for_instance(my_instance)

What I got was a ValueError telling me that i can't access m2m fields of instances that aren't saved - this was caused by a call of value_from_object from newforms/ (in newforms-admin r6710).

Wrapping the value_from_object call in a "try ... except ValueError: current_value = None" seems to have done the trick. However, I haven't submitted this as a patch, because it doesn't smell quite right. Is this the right solution? Or should value_for_object return something other than an exception for unavailable m2m field values?

Attachments (0)

Change History (1)

comment:1 Changed 6 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

This isn't a newforms-admin issue. form_for_instance is deprecated in trunk in favor of ModelForm and shouldn't be used the way you are using it. If anything you need to look at the initial kwarg to a newforms Form class to accomplish this type of behavior.

Add Comment

Modify Ticket

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

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

Note: See TracTickets for help on using tickets.