diff --git a/django/forms/fields.py b/django/forms/fields.py index b9a3d4d6c5..63ed153f0b 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -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: diff --git a/docs/releases/1.6.3.txt b/docs/releases/1.6.3.txt index a8b63dc72a..004e70f663 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 5a42e4b95d..454a8349d7 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -239,6 +239,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):