summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2009-06-03 02:37:04 +0000
committerHonza Král <honza.kral@gmail.com>2009-06-03 02:37:04 +0000
commit08652c8501196f2f9501791949934dc8a2641168 (patch)
treeac0bb54df0b9f7e93ce59914638e2bdb88115992
parente33029cca35883f6a530531396d8e452137a8024 (diff)
[soc2009/model-validation] FloatField is IntegerField with a twist
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10902 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/fields.py23
1 files changed, 8 insertions, 15 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index f835c173e9..4b135e0319 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -213,33 +213,26 @@ class IntegerField(Field):
if self.min_value is not None and value < self.min_value:
raise ValidationError(self.error_messages['min_value'] % self.min_value)
-class FloatField(Field):
+class FloatField(IntegerField):
default_error_messages = {
'invalid': _(u'Enter a number.'),
'max_value': _(u'Ensure this value is less than or equal to %s.'),
'min_value': _(u'Ensure this value is greater than or equal to %s.'),
}
- def __init__(self, max_value=None, min_value=None, *args, **kwargs):
- self.max_value, self.min_value = max_value, min_value
- Field.__init__(self, *args, **kwargs)
-
- def clean(self, value):
+ def to_python(self, value):
"""
- Validates that float() can be called on the input. Returns a float.
- Returns None for empty values.
+ Validates that float() can be called on the input. Returns the result
+ of float(). Returns None for empty values.
"""
- super(FloatField, self).clean(value)
- if not self.required and value in EMPTY_VALUES:
+ value = super(IntegerField, self).to_python(value)
+ if value in EMPTY_VALUES:
return None
+
try:
- value = float(value)
+ value = float(str(value))
except (ValueError, TypeError):
raise ValidationError(self.error_messages['invalid'])
- if self.max_value is not None and value > self.max_value:
- raise ValidationError(self.error_messages['max_value'] % self.max_value)
- if self.min_value is not None and value < self.min_value:
- raise ValidationError(self.error_messages['min_value'] % self.min_value)
return value
class DecimalField(Field):