Ticket #13971: 13971_patch.diff
File 13971_patch.diff, 3.0 KB (added by , 14 years ago) |
---|
-
django/forms/models.py
196 196 ) 197 197 return field_dict 198 198 199 def filter_declared_fields(model, declared_fields, fields=None, exclude=None): 200 """ 201 Returns an updated ``SortedDict`` filtering out the model fields 202 not included or excluded in the form definition. 203 204 """ 205 opts = model._meta 206 for f in opts.fields + opts.many_to_many: 207 exclude_field = (fields is not None and not f.name in fields) or \ 208 (exclude is not None and f.name in exclude) 209 if exclude_field and f.name in declared_fields: 210 del declared_fields[f.name] 211 return declared_fields 212 213 199 214 class ModelFormOptions(object): 200 215 def __init__(self, options=None): 201 216 self.model = getattr(options, 'model', None) … … 234 249 message = message % (', '.join(missing_fields), 235 250 opts.model.__name__) 236 251 raise FieldError(message) 252 # Filter out excluded fields from declared fields 253 declared_fields = filter_declared_fields(opts.model, 254 declared_fields, 255 opts.fields, opts.exclude) 237 256 # Override default model fields with any custom declared ones 238 257 # (plus, include all the other declared fields). 239 258 fields.update(declared_fields) -
tests/regressiontests/model_forms_regress/tests.py
442 442 self.assertTrue(form.is_valid()) 443 443 instance = construct_instance(form, Person(), fields=()) 444 444 self.assertEqual(instance.name, '') 445 446 447 class BasePublicationForm(forms.ModelForm): 448 date_published = forms.CharField() 449 class Meta: 450 model = Publication 451 452 class PublicationFormWithFields(BasePublicationForm): 453 class Meta(BasePublicationForm.Meta): 454 fields = ('title',) 455 456 class PublicationFormWithExclude(BasePublicationForm): 457 class Meta(BasePublicationForm.Meta): 458 exclude = ('date_published',) 459 460 class ExcludeCustomizedField(TestCase): 461 """Test excluded form customized model field is not added to form (#13971).""" 462 463 def test_field_is_not_added(self): 464 form = PublicationFormWithFields() 465 self.assert_('title' in form.fields) 466 self.assert_('date_published' not in form.fields) 467 self.assertEqual(len(form.fields), 1) 468 469 def test_field_is_excluded(self): 470 form = PublicationFormWithExclude() 471 self.assert_('title' in form.fields) 472 self.assert_('date_published' not in form.fields) 473 self.assertEqual(len(form.fields), 1) 474