#35342: models.fields.IntegerField.to_python() is converting float type numbers,
making .clean_fields( ) less reliable
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  thiper01@…                         |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  5.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  1
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 During the full_clean( ) routine, when an IntegerField is being cleaned
 and the code calls the field's to_python( ) method, if the value of the
 field is a float, it will just be converted to an integer using
 {{{int(value)}}}. As it is characteristic of python's native int( )
 function, it converts any float to int regardless of decimal places and
 approximations necessary, allowing for examples like this to happen:

 {{{
 exModel = ModelWithIntegerField(value=1.2)
 >>   exModel.value     #1.2
 exModel.clean_fields() #no validation error raised
 >>   exModel.value     #1
 }}}

 I don't know if this behaviour is because of ticket #24229, but I believe
 there are better ways than just allowing any number through IntegerField
 verification as long as it's a number, regardless of how it affects the
 output.

 My suggestions to improve this are:
 1# Check first if it's a float type then verify if the numbers after the
 decimal point are 0 {{{if value%1 == 0}}}, if they are, no problem
 converting, if not raise a TypeError as it would distort the original
 value without warning.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35342>
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/0107018e8c417b84-88bf72b7-8ab3-4e78-ac29-002e77a533c3-000000%40eu-central-1.amazonses.com.

Reply via email to