Opened 12 years ago

Closed 12 years ago

#19656 closed New feature (duplicate)

FloatField localization

Reported by: michael.anckaert@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: FloatField Localization
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

Currently the localization of the forms FloatField is controlled by the localize parameter to the field constructor and is independent from the project setting USE_L10N.
When using a FloatField on a model in combination with Class Based Views and a standard generated ModelForm, this produces strange results: Localization nl-BE for example displays floats as 3,2 but forms require 3.2 (comma vs dot), essentially creating an unusable UpdateView.

It would make sense to make localization default on FloatField when USE_L10N is on and use the localize parameter on FloatField to disable this.

Below code is the modified FloatField method to_python, extra change would required changing default of localize in the constructor to True.

def to_python(self, value):
        """
        Validates that float() can be called on the input. Returns the result
        of float(). Returns None for empty values.
        """
        value = super(IntegerField, self).to_python(value)
        if value in validators.EMPTY_VALUES:
            return None
        if settings.USE_L10N and self.localize:
            value = formats.sanitize_separators(value)
        try:
            value = float(value)
        except (ValueError, TypeError):
            raise ValidationError(self.error_messages['invalid'])
        return value

This change would require additional changes to the documentation to reflex the automatic localization of form fields when using USE_L10N.

Change History (2)

comment:1 by anonymous, 12 years ago

Needs documentation: set
Needs tests: set
Patch needs improvement: set

comment:2 by Claude Paroz, 12 years ago

Resolution: duplicate
Status: newclosed

Read #13032 to understand why localize is False by default. Defaulting to True for FloatField only would not be a consistent behaviour.
#13546 discusses localize issues related to ModelForms, I'd suggest you contribute to that discussion.

Somewhat related is #16612, a bug with changed detection of localized values in some fields.

Note: See TracTickets for help on using tickets.
Back to Top