So, as written on ticket #35342. During the full_clean( ) routine, when an IntegerField is being cleaned and the code calls the field’s to_python( ) method, and 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 discarding the digits right of the decimal point, 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.
This leads to a counter intuitive behaviour that may distort values being written onto the database without warning and makes avoiding this distortion awkward.
My suggestion to improve this is:
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.
This fix only stops values that would be distorted and lose information from passing clean(), it will continue to accept strings like “1” and “1.0”, and floats like 1.0
Another way to change this is to add an option to toggle this behaviour on and off.
Anyways, I would love to see what you, the community think about this.