From 9fb0f5dddc4cf7f2d294af1bcde2c359cffd90a5 Mon Sep 17 00:00:00 2001 From: Marc Tamlyn Date: Sat, 24 May 2014 10:52:18 +0100 Subject: Fixed #22510 -- Harden field removal to only None. Refs #8620. If we allow any value to remove form fields then we get name clashes with method names, media classes etc. There was a backwards incompatibility introduced meaning ModelForm subclasses with declared fields called media or clean would lose those fields. Field removal is now only permitted by using the sentinel value None. The docs have been slightly reworded to refer to removal of fields rather than shadowing. Thanks to gcbirzan for the report and initial patch, and several of the core team for opinions. --- tests/model_forms/tests.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'tests/model_forms') diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index c3b4c339c0..de96246c6f 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -2227,7 +2227,7 @@ class ModelFormInheritanceTests(TestCase): self.assertEqual(list(ModelForm().fields.keys()), ['name', 'age']) - def test_field_shadowing(self): + def test_field_removal(self): class ModelForm(forms.ModelForm): class Meta: model = Writer @@ -2250,6 +2250,24 @@ class ModelFormInheritanceTests(TestCase): self.assertEqual(list(type(str('NewForm'), (ModelForm, Form, Mixin), {})().fields.keys()), ['name', 'age']) self.assertEqual(list(type(str('NewForm'), (ModelForm, Form), {'age': None})().fields.keys()), ['name']) + def test_field_removal_name_clashes(self): + """Regression test for https://code.djangoproject.com/ticket/22510.""" + + class MyForm(forms.ModelForm): + media = forms.CharField() + + class Meta: + model = Writer + fields = '__all__' + + class SubForm(MyForm): + media = None + + self.assertIn('media', MyForm().fields) + self.assertNotIn('media', SubForm().fields) + self.assertTrue(hasattr(MyForm, 'media')) + self.assertTrue(hasattr(SubForm, 'media')) + class StumpJokeForm(forms.ModelForm): class Meta: -- cgit v1.3