[1.6.x] Fixed #22245 -- Avoided widget overwrite in forms.IntegerField subclasses

Thanks Jeroen Pulles for the report and Simon Charette for the review.
Backport of 5a976b4bec from master.
This commit is contained in:
Claude Paroz 2014-03-13 16:55:38 +01:00
parent 141bb90b4c
commit 1a2939bc26
3 changed files with 21 additions and 1 deletions

View File

@ -216,13 +216,16 @@ class CharField(Field):
class IntegerField(Field): class IntegerField(Field):
widget = NumberInput
default_error_messages = { default_error_messages = {
'invalid': _('Enter a whole number.'), 'invalid': _('Enter a whole number.'),
} }
def __init__(self, max_value=None, min_value=None, *args, **kwargs): def __init__(self, max_value=None, min_value=None, *args, **kwargs):
self.max_value, self.min_value = max_value, min_value self.max_value, self.min_value = max_value, min_value
kwargs.setdefault('widget', NumberInput if not kwargs.get('localize') else self.widget) if kwargs.get('localize') and self.widget == NumberInput:
# Localized number input is not well supported on most browsers
kwargs.setdefault('widget', super(IntegerField, self).widget)
super(IntegerField, self).__init__(*args, **kwargs) super(IntegerField, self).__init__(*args, **kwargs)
if max_value is not None: if max_value is not None:

View File

@ -18,3 +18,8 @@ several bugs in 1.6.2:
* Fixed crash of ``QuerySet``\s that use ``F() + timedelta()`` when their query * Fixed crash of ``QuerySet``\s that use ``F() + timedelta()`` when their query
was compiled more once was compiled more once
(`#21643 <http://code.djangoproject.com/ticket/21643>`_). (`#21643 <http://code.djangoproject.com/ticket/21643>`_).
* Prevented custom ``widget`` class attribute of
:class:`~django.forms.IntegerField` subclasses from being overwritten by the
code in their ``__init__`` method
(`#22245 <http://code.djangoproject.com/ticket/22245>`_).

View File

@ -234,6 +234,18 @@ class FieldsTests(SimpleTestCase):
f1 = IntegerField(localize=True) f1 = IntegerField(localize=True)
self.assertWidgetRendersTo(f1, '<input id="id_f" name="f" type="text" />') self.assertWidgetRendersTo(f1, '<input id="id_f" name="f" type="text" />')
def test_integerfield_subclass(self):
"""
Test that class-defined widget is not overwritten by __init__ (#22245).
"""
class MyIntegerField(IntegerField):
widget = Textarea
f = MyIntegerField()
self.assertEqual(f.widget.__class__, Textarea)
f = MyIntegerField(localize=True)
self.assertEqual(f.widget.__class__, Textarea)
# FloatField ################################################################## # FloatField ##################################################################
def test_floatfield_1(self): def test_floatfield_1(self):