diff --git a/django/forms/fields.py b/django/forms/fields.py index c94a5641c41..966b303ead0 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -216,13 +216,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: diff --git a/docs/releases/1.6.3.txt b/docs/releases/1.6.3.txt index a8b63dc72ae..004e70f663a 100644 --- a/docs/releases/1.6.3.txt +++ b/docs/releases/1.6.3.txt @@ -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 `_). + +* Prevented custom ``widget`` class attribute of + :class:`~django.forms.IntegerField` subclasses from being overwritten by the + code in their ``__init__`` method + (`#22245 `_). diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index f02593e4880..262ec5d9f6d 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -234,6 +234,18 @@ class FieldsTests(SimpleTestCase): f1 = IntegerField(localize=True) self.assertWidgetRendersTo(f1, '') + 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):