summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorMarc Tamlyn <marc.tamlyn@gmail.com>2014-05-24 10:52:18 +0100
committerMarc Tamlyn <marc.tamlyn@gmail.com>2014-05-24 13:11:50 +0100
commitbe733bf672eebb7d03061a293a2b0b03c727ab44 (patch)
treec0d87ca6052d1affa96432dfcbbe1122fa5efddb /tests/model_forms
parentcdfefbec721b59695e28e5992eda1cc52af48212 (diff)
[1.7.x] 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. Backport of 9fb0f5dddc4cf7f2d294af1bcde2c359cffd90a5 from master
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/tests.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 9a88ff60f0..dc912c0cd6 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -2267,7 +2267,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
@@ -2290,6 +2290,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: