mirror of
https://github.com/django/django.git
synced 2025-07-07 11:19:12 +00:00
[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
This commit is contained in:
parent
e33029cca3
commit
08652c8501
@ -213,33 +213,26 @@ class IntegerField(Field):
|
|||||||
if self.min_value is not None and value < self.min_value:
|
if self.min_value is not None and value < self.min_value:
|
||||||
raise ValidationError(self.error_messages['min_value'] % self.min_value)
|
raise ValidationError(self.error_messages['min_value'] % self.min_value)
|
||||||
|
|
||||||
class FloatField(Field):
|
class FloatField(IntegerField):
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
'invalid': _(u'Enter a number.'),
|
'invalid': _(u'Enter a number.'),
|
||||||
'max_value': _(u'Ensure this value is less than or equal to %s.'),
|
'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.'),
|
'min_value': _(u'Ensure this value is greater than or equal to %s.'),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, max_value=None, min_value=None, *args, **kwargs):
|
def to_python(self, value):
|
||||||
self.max_value, self.min_value = max_value, min_value
|
|
||||||
Field.__init__(self, *args, **kwargs)
|
|
||||||
|
|
||||||
def clean(self, value):
|
|
||||||
"""
|
"""
|
||||||
Validates that float() can be called on the input. Returns a float.
|
Validates that float() can be called on the input. Returns the result
|
||||||
Returns None for empty values.
|
of float(). Returns None for empty values.
|
||||||
"""
|
"""
|
||||||
super(FloatField, self).clean(value)
|
value = super(IntegerField, self).to_python(value)
|
||||||
if not self.required and value in EMPTY_VALUES:
|
if value in EMPTY_VALUES:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
value = float(value)
|
value = float(str(value))
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
raise ValidationError(self.error_messages['invalid'])
|
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
|
return value
|
||||||
|
|
||||||
class DecimalField(Field):
|
class DecimalField(Field):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user