Ticket #17657: issue17657.diff
File issue17657.diff, 3.3 KB (added by , 13 years ago) |
---|
-
django/forms/models.py
124 124 if instance.pk is None: 125 125 data[f.name] = [] 126 126 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)) 129 128 else: 130 129 data[f.name] = f.value_from_object(instance) 131 130 return data -
tests/regressiontests/forms/tests/__init__.py
13 13 CustomDateTimeInputFormatsTests, SimpleDateTimeFormatTests) 14 14 from .media import FormsMediaTestCase, StaticFormsMediaTestCase 15 15 from .models import (TestTicket12510, ModelFormCallableModelDefault, 16 FormsModelTestCase, RelatedModelFormTests) 16 FormsModelTestCase, RelatedModelFormTests, 17 MultipleChoiceWithFieldName) 17 18 from .regressions import FormsRegressionsTestCase 18 19 from .util import FormsUtilTestCase 19 20 from .validators import TestFieldWithValidators -
tests/regressiontests/forms/tests/models.py
5 5 6 6 from django.core.files.uploadedfile import SimpleUploadedFile 7 7 from django.db import models 8 from django.forms import Form, ModelForm, FileField, ModelChoiceField 8 from django.forms import Form, ModelForm, FileField, ModelChoiceField, ModelMultipleChoiceField 9 9 from django.forms.models import ModelFormMetaclass 10 10 from django.test import TestCase 11 11 … … 18 18 model = ChoiceFieldModel 19 19 20 20 21 class ChoiceFieldFormWithFieldName(ModelForm): 22 23 multi_choice = ModelMultipleChoiceField(ChoiceOptionModel.objects.all(), 24 to_field_name="name") 25 26 class Meta: 27 model = ChoiceFieldModel 28 29 21 30 class FileForm(Form): 22 31 file1 = FileField() 23 32 … … 196 205 model=A 197 206 198 207 self.assertTrue(issubclass(ModelFormMetaclass('Form', (ModelForm,), {'Meta': Meta}), ModelForm)) 208 209 class 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>""")