Opened 7 months ago
Closed 7 months ago
#35342 closed Bug (wontfix)
models.fields.IntegerField.to_python() is converting float type numbers, making .clean_fields( ) less reliable
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
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.
Change History (2)
comment:1 by , 7 months ago
Has patch: | set |
---|
comment:2 by , 7 months ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Hi there,
The issue tracker is for reporting bugs with Django, whereas this request is change in known behaviour that folks may be relying upon. Please start a discussion on the Django forum if you strongly feel this needs to change: https://www.djangoproject.com/community/ If the Django community agrees then this ticket can be reopened.
Closing per https://code.djangoproject.com/wiki/DevelopersMailingList#BeenWONTFIXed
https://github.com/django/django/pull/18033