Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Fixed #22245 -- Avoided widget overwrite in forms.IntegerFiel…

…d subclasses

Thanks Jeroen Pulles for the report and Simon Charette for the review.
Backport of 5a976b4 from master.
  • Loading branch information...
commit 1a2939bc26b7077e3196cea9bce52826ec7a6d9d 1 parent 141bb90
Claude Paroz claudep authored
5 django/forms/fields.py
View
@@ -216,13 +216,16 @@ def widget_attrs(self, widget):
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:
5 docs/releases/1.6.3.txt
View
@@ -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>`_).
12 tests/forms_tests/tests/test_fields.py
View
@@ -234,6 +234,18 @@ def test_integerfield_localized(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.