Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #5355 -- Fixed data cleaning for DecimalField.

In passing, fixed a problem with cleaning in IntegerField.

Includes tests from PhiR.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6282 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 885db3cb793ac1a7c269a67b5739dfc4dbe6214d 1 parent 7ca1a04
@malcolmt malcolmt authored
Showing with 16 additions and 2 deletions.
  1. +2 −2 django/newforms/fields.py
  2. +14 −0 tests/regressiontests/forms/tests.py
View
4 django/newforms/fields.py
@@ -144,7 +144,7 @@ def clean(self, value):
if value in EMPTY_VALUES:
return None
try:
- value = int(value)
+ value = int(str(value))
except (ValueError, TypeError):
raise ValidationError(ugettext(u'Enter a whole number.'))
if self.max_value is not None and value > self.max_value:
@@ -192,7 +192,7 @@ def clean(self, value):
super(DecimalField, self).clean(value)
if not self.required and value in EMPTY_VALUES:
return None
- value = value.strip()
+ value = str(value).strip()
try:
value = Decimal(value)
except DecimalException:
View
14 tests/regressiontests/forms/tests.py
@@ -961,6 +961,12 @@
Traceback (most recent call last):
...
ValidationError: [u'Enter a whole number.']
+>>> f.clean(42)
+42
+>>> f.clean(3.14)
+Traceback (most recent call last):
+...
+ValidationError: [u'Enter a whole number.']
>>> f.clean('1 ')
1
>>> f.clean(' 1')
@@ -1084,6 +1090,10 @@
23.0
>>> f.clean('3.14')
3.1400000000000001
+>>> f.clean(3.14)
+3.1400000000000001
+>>> f.clean(42)
+42.0
>>> f.clean('a')
Traceback (most recent call last):
...
@@ -1142,6 +1152,10 @@
Decimal("23")
>>> f.clean('3.14')
Decimal("3.14")
+>>> f.clean(3.14)
+Decimal("3.14")
+>>> f.clean(Decimal('3.14'))
+Decimal("3.14")
>>> f.clean('a')
Traceback (most recent call last):
...
Please sign in to comment.
Something went wrong with that request. Please try again.