#2380: ChangeManipulator's and ManyToManyFields (do not show up in resulting
FormWrapper)
---------------------------------+------------------------------------------
Reporter: [EMAIL PROTECTED] | Owner: jacob
Type: defect | Status: new
Priority: normal | Milestone:
Component: Documentation | Version:
Severity: normal | Keywords:
---------------------------------+------------------------------------------
I have a model that has in it a ManyToManyField that is editable.
I need to present a form that lets you add and remove items from this
ManyToManyField.
I figured using the rote ChangeManipulator would work but something was
wrong. My form field for the ManyToMany field never had the default data
from the object in it. After much head scratching I had a 'duh' momment.
The example code goes like this:
{{{
def view(...):
try:
manipulator = klass.ChangeManipulator(obj_id)
except ObjectDoesNotExist:
raise Http404
obj = manipulator.original_object
if request.POST:
....
else:
errors = {}
new_data = obj.__dict__
form = forms.FormWrapper(manipulator, new_data, errors)
...
}}}
and the '''new_data = obj.__dict__''' is the problem. Since this is a
ManyToManyField it actually has no key in '''obj.__dict__'''. Copying the
dictionary will not copy the existing data for this field.
Now, for a kludge I have:
{{{
...
else:
# This was NOT a POST. We want to just display the object with any
# form fields filled in from the actual object's data, with no
errors
# in the form.
#
errors = {}
new_data = obj.__dict__
new_data['fixed_address'] = obj.fixed_address.all()
...
}}}
and voila, my data appears pre-filled in in the form. I see and know how
this works but I have to think that something is wrong here. For now,
since this is in a generic
function I am going to iterate over the fields in
'''obj.__class__.meta.fields''' look for ones that are ManyToManyField and
'''editable = True''' and push '''getattr(obj, field_name).all()''' in to
the new_data dictionary, but am I missing how I am supposed to be doing
this? If nothing else I am thinking that the docs should mention that you
can not use the example if you have an editable ManyToManyField.
--
Ticket URL: <http://code.djangoproject.com/ticket/2380>
Django <http://code.djangoproject.org/>
The web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---