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

[soc2009/model-validation] DecimalField done

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10903 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-06-03 02:37:20 +00:00
parent 08652c8501
commit 795cd55ba2

View File

@ -208,6 +208,8 @@ class IntegerField(Field):
def validate(self, value): def validate(self, value):
super(IntegerField, self).validate(value) super(IntegerField, self).validate(value)
if value in EMPTY_VALUES:
return
if self.max_value is not None and value > self.max_value: if self.max_value is not None and value > self.max_value:
raise ValidationError(self.error_messages['max_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: if self.min_value is not None and value < self.min_value:
@ -250,22 +252,26 @@ class DecimalField(Field):
self.max_digits, self.decimal_places = max_digits, decimal_places self.max_digits, self.decimal_places = max_digits, decimal_places
Field.__init__(self, *args, **kwargs) Field.__init__(self, *args, **kwargs)
def clean(self, value): def to_python(self, value):
""" """
Validates that the input is a decimal number. Returns a Decimal Validates that the input is a decimal number. Returns a Decimal
instance. Returns None for empty values. Ensures that there are no more instance. Returns None for empty values. Ensures that there are no more
than max_digits in the number, and no more than decimal_places digits than max_digits in the number, and no more than decimal_places digits
after the decimal point. after the decimal point.
""" """
super(DecimalField, self).clean(value) if value in EMPTY_VALUES:
if not self.required and value in EMPTY_VALUES:
return None return None
value = smart_str(value).strip() value = smart_str(value).strip()
try: try:
value = Decimal(value) value = Decimal(value)
except DecimalException: except DecimalException:
raise ValidationError(self.error_messages['invalid']) raise ValidationError(self.error_messages['invalid'])
return value
def validate(self, value):
super(DecimalField, self).validate(value)
if value in EMPTY_VALUES:
return
sign, digittuple, exponent = value.as_tuple() sign, digittuple, exponent = value.as_tuple()
decimals = abs(exponent) decimals = abs(exponent)
# digittuple doesn't include any leading zeros. # digittuple doesn't include any leading zeros.