﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
2380	ChangeManipulator's and ManyToManyFields (do not show up in resulting FormWrapper)	scanner@…	Jacob	"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.

"	defect	closed	Documentation		normal	wontfix			Unreviewed	0	0	0	0	0	0
