Ticket #14119: fix-14119.diff
File fix-14119.diff, 3.7 KB (added by , 14 years ago) |
---|
-
django/forms/models.py
39 39 if not f.editable or isinstance(f, models.AutoField) \ 40 40 or not f.name in cleaned_data: 41 41 continue 42 if fields and f.name not in fields:42 if fields is not None and f.name not in fields: 43 43 continue 44 44 if exclude and f.name in exclude: 45 45 continue … … 167 167 for f in opts.fields + opts.many_to_many: 168 168 if not f.editable: 169 169 continue 170 if fields and not f.name in fields:170 if fields is not None and not f.name in fields: 171 171 continue 172 172 if exclude and f.name in exclude: 173 173 continue -
tests/regressiontests/model_forms_regress/tests.py
2 2 3 3 from django import db 4 4 from django import forms 5 from django.forms.models import modelform_factory, ModelChoiceField 5 from django.forms.models import modelform_factory, ModelChoiceField, fields_for_model, construct_instance 6 6 from django.conf import settings 7 7 from django.test import TestCase 8 8 … … 250 250 form.is_valid() 251 251 # self.assertTrue(form.is_valid()) 252 252 # self.assertEquals(form.cleaned_data['url'], 'http://example.com/test') 253 254 class EmptyFieldsTestCase(TestCase): 255 "Tests for fields=() cases as reported in #14119" 256 class EmptyPersonForm(forms.ModelForm): 257 class Meta: 258 model = Person 259 fields = () 260 261 def test_empty_fields_to_fields_for_model(self): 262 "An argument of fields=() to fields_for_model should return an empty dictionary" 263 field_dict = fields_for_model(Person, fields=()) 264 self.assertEqual(len(field_dict), 0) 265 266 def test_empty_fields_on_modelform(self): 267 "No fields on a ModelForm should actually result in no fields" 268 form = self.EmptyPersonForm() 269 self.assertEqual(len(form.fields), 0) 270 271 def test_empty_fields_to_construct_instance(self): 272 "No fields should be set on a model instance if construct_instance receives fields=()" 273 form = modelform_factory(Person)({'name': 'John Doe'}) 274 self.assertTrue(form.is_valid()) 275 instance = construct_instance(form, Person(), fields=()) 276 self.assertEqual(instance.name, '') -
tests/regressiontests/model_formsets_regress/tests.py
1 from django.forms.models import modelform_factory, inlineformset_factory1 from django.forms.models import modelform_factory, modelformset_factory, inlineformset_factory 2 2 from django.test import TestCase 3 3 4 4 from models import User, UserSite, Restaurant, Manager … … 156 156 # you can create a formset with an instance of None 157 157 form = Form(instance=None) 158 158 formset = FormSet(instance=None) 159 160 def test_empty_fields_on_modelformset(self): 161 "No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119." 162 UserFormSet = modelformset_factory(User, fields=()) 163 formset = UserFormSet() 164 for form in formset.forms: 165 self.assertTrue('id' in form.fields) 166 self.assertEqual(len(form.fields), 1)