[Django] #35342: models.fields.IntegerField.to_python() is converting float type numbers, making .clean_fields( ) less reliable

11 views
Skip to first unread message

Django

unread,
Mar 29, 2024, 6:08:10 PM3/29/24
to django-...@googlegroups.com
#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.

Django

unread,
Mar 29, 2024, 6:22:43 PM3/29/24
to django-...@googlegroups.com
#35342: models.fields.IntegerField.to_python() is converting float type numbers,
making .clean_fields( ) less reliable
-------------------------------------+-------------------------------------
Reporter: thiper01@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by thiper01@…):

* has_patch: 0 => 1

Comment:

https://github.com/django/django/pull/18033
--
Ticket URL: <https://code.djangoproject.com/ticket/35342#comment:1>

Django

unread,
Mar 29, 2024, 6:53:43 PM3/29/24
to django-...@googlegroups.com
#35342: models.fields.IntegerField.to_python() is converting float type numbers,
making .clean_fields( ) less reliable
-------------------------------------+-------------------------------------
Reporter: thiper01@… | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Sanders):

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

Comment:

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