1
0
mirror of https://github.com/django/django.git synced 2025-07-07 11:19:12 +00:00

[soc2009/model-validation] IntegerField migrated to split to_python/validate

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10901 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-06-03 02:36:49 +00:00
parent 45af711718
commit e33029cca3

View File

@ -191,23 +191,27 @@ class IntegerField(Field):
self.max_value, self.min_value = max_value, min_value
super(IntegerField, self).__init__(*args, **kwargs)
def clean(self, value):
def to_python(self, value):
"""
Validates that int() can be called on the input. Returns the result
of int(). Returns None for empty values.
"""
super(IntegerField, self).clean(value)
value = super(IntegerField, self).to_python(value)
if value in EMPTY_VALUES:
return None
try:
value = int(str(value))
except (ValueError, TypeError):
raise ValidationError(self.error_messages['invalid'])
return value
def validate(self, value):
super(IntegerField, self).validate(value)
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 FloatField(Field):
default_error_messages = {