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.
* 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>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/24229#comment:2>
* 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>