Opened 11 years ago

Closed 11 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: no UI/UX: no


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?

Change History (1)

comment:1 Changed 11 years ago by Brian Rosner

Resolution: invalid
Status: newclosed

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.

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