Ticket #8620: django_model_forms_excluding_fields_fix_2.diff
File django_model_forms_excluding_fields_fix_2.diff, 2.4 KB (added by , 14 years ago) |
---|
-
django/forms/models.py
214 214 raise FieldError(message) 215 215 # Override default model fields with any custom declared ones 216 216 # (plus, include all the other declared fields). 217 if opts.fields: 218 [declared_fields.pop(f) for f in declared_fields.keys() if f not in opts.fields] 219 if opts.exclude: 220 [declared_fields.pop(f) for f in declared_fields.keys() if f in opts.exclude] 217 221 fields.update(declared_fields) 218 222 else: 219 223 fields = declared_fields -
tests/regressiontests/forms/tests/models.py
1 1 # -*- coding: utf-8 -*- 2 2 import datetime 3 3 from django.core.files.uploadedfile import SimpleUploadedFile 4 from django.forms import Form, ModelForm, FileField, ModelChoiceField 4 from django.forms import Form, ModelForm, FileField, ModelChoiceField, CharField 5 5 from django.test import TestCase 6 6 from regressiontests.forms.models import ChoiceModel, ChoiceOptionModel, ChoiceFieldModel, FileModel, Group, BoundaryModel, Defaults 7 7 … … 154 154 155 155 f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)}) 156 156 self.assertTrue(f.is_valid()) 157 self.assert Equal(f.cleaned_data['name'], u'Hello')157 self.assertNotIn('name', f.cleaned_data) 158 158 obj = f.save() 159 159 self.assertEqual(obj.name, u'class default value') 160 160 self.assertEqual(obj.value, 99) 161 161 self.assertEqual(obj.def_date, datetime.date(1999, 3, 2)) 162 163 164 class TestTicket8620(TestCase): 165 '''Allow exclusion of not only model fields but also form fields (as 166 might be defined in a superclass) using the exclude Meta attribute.''' 167 168 class ParentForm(ModelForm): 169 extra_non_model_field = CharField() 170 171 class ChildForm(ParentForm): 172 class Meta: 173 model = Defaults # any model is suitable for this test 174 exclude = ('extra_non_model_field',) 175 176 def test_extra_non_model_field_is_excluded(self): 177 form = TestTicket8620.ChildForm() 178 self.assertNotIn('extra_non_model_field', form.fields)