Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix regression that removes field whose name is present on the form c…

…lass as an attribute.
  • Loading branch information...
commit f62f991444610ebadad809981d9a037298a660db 1 parent 2520971
@gcbirzan authored
Showing with 19 additions and 5 deletions.
  1. +5 −5 django/forms/forms.py
  2. +14 −0 tests/model_forms/tests.py
View
10 django/forms/forms.py
@@ -86,15 +86,15 @@ def __new__(mcs, name, bases, attrs):
# Walk through the MRO.
declared_fields = OrderedDict()
for base in reversed(new_class.__mro__):
+ # Field shadowing.
+ for attr in declared_fields:
+ if base.__dict__.get(attr, True) is None:
+ declared_fields.pop(attr)
+
# Collect fields from base class.
if hasattr(base, 'declared_fields'):
declared_fields.update(base.declared_fields)
- # Field shadowing.
- for attr in base.__dict__.keys():
- if attr in declared_fields:
- declared_fields.pop(attr)
-
new_class.base_fields = declared_fields
new_class.declared_fields = declared_fields
View
14 tests/model_forms/tests.py
@@ -2250,6 +2250,20 @@ class Form2(forms.Form):
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_shadowing_extra(self):
+ class Form(forms.Form):
+ media = forms.IntegerField()
+
+ class Form2(forms.Form):
+ pass
+
+ class Form3(forms.Form):
+ media = 42
+
+ self.assertEqual(list(Form().fields.keys()), ['media'])
+ self.assertEqual(list(type(str('NewForm'), (Form2, Form), {})().fields.keys()), ['media'])
+ self.assertEqual(list(type(str('NewForm'), (Form3, Form), {})().fields.keys()), ['media'])
+
class StumpJokeForm(forms.ModelForm):
class Meta:
Please sign in to comment.
Something went wrong with that request. Please try again.