mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
[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 9fb0f5dddc from master
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user