Ticket #22245: 22245-1.diff

File 22245-1.diff, 1.8 KB (added by claudep, 17 months ago)

Suggested fix

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index b9a3d4d..63ed153 100644
    a b class CharField(Field): 
    231231
    232232
    233233class IntegerField(Field):
     234    widget = NumberInput
    234235    default_error_messages = {
    235236        'invalid': _('Enter a whole number.'),
    236237    }
    237238
    238239    def __init__(self, max_value=None, min_value=None, *args, **kwargs):
    239240        self.max_value, self.min_value = max_value, min_value
    240         kwargs.setdefault('widget', NumberInput if not kwargs.get('localize') else self.widget)
     241        if kwargs.get('localize') and self.widget == NumberInput:
     242            # Localized number input is not well supported on most browsers
     243            kwargs.setdefault('widget', super(IntegerField, self).widget)
    241244        super(IntegerField, self).__init__(*args, **kwargs)
    242245
    243246        if max_value is not None:
  • tests/forms_tests/tests/test_fields.py

    diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py
    index 5a42e4b..454a834 100644
    a b class FieldsTests(SimpleTestCase): 
    239239        f1 = IntegerField(localize=True)
    240240        self.assertWidgetRendersTo(f1, '<input id="id_f" name="f" type="text" />')
    241241
     242    def test_integerfield_subclass(self):
     243        """
     244        Test that class-defined widget is not overwritten by __init__ (#22245).
     245        """
     246        class MyIntegerField(IntegerField):
     247            widget = Textarea
     248
     249        f = MyIntegerField()
     250        self.assertEqual(f.widget.__class__, Textarea)
     251        f = MyIntegerField(localize=True)
     252        self.assertEqual(f.widget.__class__, Textarea)
     253
    242254    # FloatField ##################################################################
    243255
    244256    def test_floatfield_1(self):
Back to Top