Fixed #22245 -- Avoided widget overwrite in forms.IntegerField subclasses
Thanks Jeroen Pulles for the report and Simon Charette for the review.
This commit is contained in:
parent
958b511a80
commit
5a976b4bec
|
@ -231,13 +231,16 @@ class CharField(Field):
|
|||
|
||||
|
||||
class IntegerField(Field):
|
||||
widget = NumberInput
|
||||
default_error_messages = {
|
||||
'invalid': _('Enter a whole number.'),
|
||||
}
|
||||
|
||||
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
|
||||
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)
|
||||
|
||||
if max_value is not None:
|
||||
|
|
|
@ -18,3 +18,8 @@ several bugs in 1.6.2:
|
|||
* Fixed crash of ``QuerySet``\s that use ``F() + timedelta()`` when their query
|
||||
was compiled more once
|
||||
(`#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>`_).
|
||||
|
|
|
@ -239,6 +239,18 @@ class FieldsTests(SimpleTestCase):
|
|||
f1 = IntegerField(localize=True)
|
||||
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 ##################################################################
|
||||
|
||||
def test_floatfield_1(self):
|
||||
|
|
Loading…
Reference in New Issue