Ticket #17657: issue17657.diff

File issue17657.diff, 3.3 KB (added by Andrey Fedoseev, 13 years ago)
  • django/forms/models.py

     
    124124            if instance.pk is None:
    125125                data[f.name] = []
    126126            else:
    127                 # MultipleChoiceWidget needs a list of pks, not object instances.
    128                 data[f.name] = [obj.pk for obj in f.value_from_object(instance)]
     127                data[f.name] = list(f.value_from_object(instance))
    129128        else:
    130129            data[f.name] = f.value_from_object(instance)
    131130    return data
  • tests/regressiontests/forms/tests/__init__.py

     
    1313    CustomDateTimeInputFormatsTests, SimpleDateTimeFormatTests)
    1414from .media import FormsMediaTestCase, StaticFormsMediaTestCase
    1515from .models import (TestTicket12510, ModelFormCallableModelDefault,
    16     FormsModelTestCase, RelatedModelFormTests)
     16    FormsModelTestCase, RelatedModelFormTests,
     17    MultipleChoiceWithFieldName)
    1718from .regressions import FormsRegressionsTestCase
    1819from .util import FormsUtilTestCase
    1920from .validators import TestFieldWithValidators
  • tests/regressiontests/forms/tests/models.py

     
    55
    66from django.core.files.uploadedfile import SimpleUploadedFile
    77from django.db import models
    8 from django.forms import Form, ModelForm, FileField, ModelChoiceField
     8from django.forms import Form, ModelForm, FileField, ModelChoiceField, ModelMultipleChoiceField
    99from django.forms.models import ModelFormMetaclass
    1010from django.test import TestCase
    1111
     
    1818        model = ChoiceFieldModel
    1919
    2020
     21class ChoiceFieldFormWithFieldName(ModelForm):
     22
     23    multi_choice = ModelMultipleChoiceField(ChoiceOptionModel.objects.all(),
     24                                            to_field_name="name")
     25
     26    class Meta:
     27        model = ChoiceFieldModel
     28
     29
    2130class FileForm(Form):
    2231    file1 = FileField()
    2332
     
    196205            model=A
    197206
    198207        self.assertTrue(issubclass(ModelFormMetaclass('Form', (ModelForm,), {'Meta': Meta}), ModelForm))
     208
     209class MultipleChoiceWithFieldName(TestCase):
     210
     211    def test_to_field_name(self):
     212        """
     213        Test for issue 17657
     214        """
     215        obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
     216        obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
     217        obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
     218        instance = ChoiceFieldModel.objects.create()
     219
     220        instance.multi_choice.add(obj2, obj3)
     221
     222        form = ChoiceFieldFormWithFieldName(instance=instance)
     223
     224        self.assertHTMLEqual(
     225            str(form["multi_choice"]), """<select multiple="multiple" name="multi_choice" id="id_multi_choice">
     226<option value="default">ChoiceOption 1</option>
     227<option value="option 2" selected="selected">ChoiceOption 2</option>
     228<option value="option 3" selected="selected">ChoiceOption 3</option>
     229</select>""")
Back to Top