Fixed #7064: Made DemicmalField validation support max_digits equal to decimal_places.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9387 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2008-11-10 19:52:53 +00:00
parent 8cdc53a265
commit d82aaef844
2 changed files with 13 additions and 12 deletions

View File

@ -254,12 +254,12 @@ class DecimalField(Field):
decimals = abs(exponent) decimals = abs(exponent)
# digittuple doesn't include any leading zeros. # digittuple doesn't include any leading zeros.
digits = len(digittuple) digits = len(digittuple)
if decimals >= digits: if decimals > digits:
# We have leading zeros up to or past the decimal point. Count # We have leading zeros up to or past the decimal point. Count
# everything past the decimal point as a digit. We also add one # everything past the decimal point as a digit. We do not count
# for leading zeros before the decimal point (any number of leading # 0 before the decimal point as a digit since that would mean
# whole zeros collapse to one digit). # we would not allow max_digits = decimal_places.
digits = decimals + 1 digits = decimals
whole_digits = digits - decimals whole_digits = digits - decimals
if self.max_value is not None and value > self.max_value: if self.max_value is not None and value > self.max_value:

View File

@ -366,7 +366,7 @@ True
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Ensure that there are no more than 2 decimal places.'] ValidationError: [u'Ensure that there are no more than 2 decimal places.']
>>> f.clean('-000.1234') >>> f.clean('-000.12345')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Ensure that there are no more than 4 digits in total.'] ValidationError: [u'Ensure that there are no more than 4 digits in total.']
@ -416,19 +416,20 @@ ValidationError: [u'Ensure that there are no more than 2 decimal places.']
# Leading whole zeros "collapse" to one digit. # Leading whole zeros "collapse" to one digit.
>>> f.clean('0000000.10') == Decimal("0.1") >>> f.clean('0000000.10') == Decimal("0.1")
True True
>>> f.clean('0000000.100')
Traceback (most recent call last): # But a leading 0 before the . doesn't count towards max_digits
... >>> f.clean('0000000.100') == Decimal("0.100")
ValidationError: [u'Ensure that there are no more than 3 digits in total.'] True
# Only leading whole zeros "collapse" to one digit. # Only leading whole zeros "collapse" to one digit.
>>> f.clean('000000.02') == Decimal('0.02') >>> f.clean('000000.02') == Decimal('0.02')
True True
>>> f.clean('000000.002') >>> f.clean('000000.0002')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Ensure that there are no more than 3 digits in total.'] ValidationError: [u'Ensure that there are no more than 3 digits in total.']
>>> f.clean('.002') == Decimal("0.002")
True
# DateField ################################################################### # DateField ###################################################################