Opened 8 months ago
Closed 8 months ago
#35304 closed Uncategorized (invalid)
IntegerField trailing decimal and zeros
Reported by: | Piotr Kotarba | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a little concern in regard of stripping trailing decimal and zeros.
IntegerField Uses NumberInput as widget, which allows to pass Numbers with trailing zeros.
For example, below numbers are valid:
10
10.00
10,00
Shouldn't IntegerField accept only Integers?
Below method uses _lazy_re_compile to get rid of them.
def to_python(self, value): """ Validate that int() can be called on the input. Return the result of int() or None for empty values. """ value = super().to_python(value) if value in self.empty_values: return None if self.localize: value = formats.sanitize_separators(value) # Strip trailing decimal and zeros. try: value = int(self.re_decimal.sub('', str(value))) except (ValueError, TypeError): raise ValidationError(self.error_messages['invalid'], code='invalid') return value
I have found related issue:
https://code.djangoproject.com/ticket/24229
The reason of this functionality as I understand was:
"Django forms are useful for validating user input beyond just HTML forms."
"One issue, MS Excel represents all numbers in cells as floats, even integers. So for this to work, a float with value 1.0 should be cleaned by forms.IntegerField as 1. "
I am sceptic to this solution, and as Django main purpose is to be used on HTML, it shouldn't have such functionality in its core.
Also I think that this issue was created when there was no inputs, and HTML looked different as well. Lots have changed since then.
What do you think?
Hi there 👋
Trac is for reporting bugs with Django. If you just want to start a discussion on something you'll need to head over to Discord or the Forum, details here: https://www.djangoproject.com/community/
Thanks!