﻿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
17657	ModelForm does not respect ModelMultipleChoiceField's to_field_name attribute	Andrey Fedoseev	nobody	"When extracting data from existing model instance `ModelForm` ignores `to_field_name` attribute of `ModelMultipleChoiceField` fields and always extracts values as a list of primary keys (see https://code.djangoproject.com/browser/django/trunk/django/forms/models.py?rev=17434#L127)

One of the problems caused by this behaviour is that options in multiple select widget are not selected. Here's a small test case to illustrate this:

{{{
# models.py

from django.db import models


class Foo(models.Model):

    slug = models.CharField(max_length=40, unique=True)
    title = models.CharField(max_length=40, unique=True)


class Bar(models.Model):

    foos = models.ManyToManyField(Foo)

}}}


{{{
# tests.py

from django.test.testcases import TestCase
from django import forms
from models import Foo, Bar


class TestModelMultipleChoiceWithFieldName(TestCase):

    def setUp(self):
        self.instance = Bar.objects.create()

        self.instance.foos.add(Foo.objects.create(title=""Spam"", slug=""spam""))
        self.instance.foos.add(Foo.objects.create(title=""Ham"", slug=""ham""))
        self.instance.foos.add(Foo.objects.create(title=""Eggs"", slug=""eggs""))


    def test_without_field_name(self):

        class Form(forms.ModelForm):

            foos = forms.ModelMultipleChoiceField(Foo.objects.all())

            class Meta:
                model = Bar

        form = Form(instance=self.instance)

        self.assertEquals(
            str(form[""foos""]),
            '<select multiple=""multiple"" name=""foos"" id=""id_foos"">\n'
            '<option value=""1"" selected=""selected"">Spam</option>\n'
            '<option value=""2"" selected=""selected"">Ham</option>\n'
            '<option value=""3"" selected=""selected"">Eggs</option>\n'
            '</select>'
        )

    def test_with_field_name(self):

        class Form(forms.ModelForm):

            foos = forms.ModelMultipleChoiceField(Foo.objects.all(), to_field_name=""slug"")

            class Meta:
                model = Bar

        form = Form(instance=self.instance)

        # Fails! Options aren't selected.
        self.assertEquals(
            str(form[""foos""]),
            '<select multiple=""multiple"" name=""foos"" id=""id_foos"">\n'
            '<option value=""spam"" selected=""selected"">Spam</option>\n'
            '<option value=""ham"" selected=""selected"">Ham</option>\n'
            '<option value=""eggs"" selected=""selected"">Eggs</option>\n'
            '</select>'
        )

}}}

I am preparing a patch now."	Bug	closed	Forms	1.3	Normal	fixed		marc.tamlyn@… bmispelon@… valtron2000@… nav@… andrey.fedoseev@…	Accepted	1	0	0	1	0	0
