#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e420e814a-89119f2a-9b9f-4b48-bac7-d26c040b4f43-000000%40eu-central-1.amazonses.com.

Reply via email to