[Django] #35304: IntegerField trailing decimal and zeros

24 views
Skip to first unread message

Django

unread,
Mar 15, 2024, 8:20:35 AM3/15/24
to django-...@googlegroups.com
#35304: IntegerField trailing decimal and zeros
-----------------------------------------+------------------------
Reporter: Piotr Kotarba | Owner: nobody
Type: Uncategorized | Status: new
Component: Forms | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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?
--
Ticket URL: <https://code.djangoproject.com/ticket/35304>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 15, 2024, 8:48:58 AM3/15/24
to django-...@googlegroups.com
#35304: IntegerField trailing decimal and zeros
-------------------------------+--------------------------------------
Reporter: Piotr Kotarba | Owner: nobody
Type: Uncategorized | Status: closed
Component: Forms | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by David Sanders):

* resolution: => invalid
* status: new => closed

Comment:

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!
--
Ticket URL: <https://code.djangoproject.com/ticket/35304#comment:1>
Reply all
Reply to author
Forward
0 new messages