m2m ValueError with form_for_instance on fresh instance
|Reported by:||sjulean||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||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/models.py:119 (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?