[Django] #24229: forms.IntegerField.clean() fails to clean float objects that are integers

14 views
Skip to first unread message

Django

unread,
Jan 27, 2015, 2:10:59 PM1/27/15
to django-...@googlegroups.com
#24229: forms.IntegerField.clean() fails to clean float objects that are integers
-----------------------------+--------------------
Reporter: jdufresne | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------
Django forms are useful for validating user input beyond just HTML forms.
They can be used to validate and clean user input from multiple sources.

One source could potentially be an spreadsheets. Imagine a spreadsheet
where each row is data keyed by its header. Each row is validated by a
Django `Form` object, if the form is valid, some action occurs.

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`. Currently this fails as
`forms.IntegerField.to_python()` is implemeted as:

{{{
try:
value = int(str(value))
except (ValueError, TypeError):
raise ValidationError(self.error_messages['invalid'], code='invalid')
}}}

But this fails for floats:

{{{
$ python -c 'int(str(1.0))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.0'
}}}

The following test demonstrates that this does not work in Django:

{{{
$ git diff
diff --git a/tests/forms_tests/tests/test_fields.py
b/tests/forms_tests/tests/test_fields.py
index fda4512..3069520 100644
--- a/tests/forms_tests/tests/test_fields.py
+++ b/tests/forms_tests/tests/test_fields.py
@@ -184,6 +184,7 @@ class FieldsTests(SimpleTestCase):
self.assertRaisesMessage(ValidationError, "'Enter a whole
number.'", f.clean, '1a')
self.assertEqual(f.max_value, None)
self.assertEqual(f.min_value, None)
+ self.assertEqual(1, f.clean(1.0))

def test_integerfield_2(self):
f = IntegerField(required=False)
}}}

I propose that the `fields.IntegerField` be able to clean float objects
that represent an integer. This will help make Django forms useful for
user input beyond simple HTML forms.

--
Ticket URL: <https://code.djangoproject.com/ticket/24229>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 27, 2015, 4:07:29 PM1/27/15
to django-...@googlegroups.com
#24229: forms.IntegerField.clean() fails to clean float objects that are integers
-----------------------------+--------------------------------------

Reporter: jdufresne | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by jdufresne):

* needs_better_patch: => 0
* has_patch: 0 => 1
* needs_tests: => 0
* needs_docs: => 0


Comment:

https://github.com/django/django/pull/3995

--
Ticket URL: <https://code.djangoproject.com/ticket/24229#comment:1>

Django

unread,
Jan 27, 2015, 6:14:13 PM1/27/15
to django-...@googlegroups.com
#24229: forms.IntegerField.clean() fails to clean float objects that are integers
-----------------------------+------------------------------------

Reporter: jdufresne | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by wimfeijen):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24229#comment:2>

Django

unread,
Feb 27, 2015, 6:24:36 AM2/27/15
to django-...@googlegroups.com
#24229: forms.IntegerField.clean() fails to clean float objects that are integers
-----------------------------+------------------------------------
Reporter: jdufresne | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Claude Paroz <claude@…>):

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


Comment:

In [changeset:"3b966c2b73c420222d1524cda03c1a5626c90fe6"]:
{{{
#!CommitTicketReference repository=""
revision="3b966c2b73c420222d1524cda03c1a5626c90fe6"
Fixed #24229 -- Changed IntegerField to clean floats representing integers
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24229#comment:3>

Reply all
Reply to author
Forward
0 new messages