Fixed DecimalField arbitrary precision support (#4075)

This commit is contained in:
Daniele Varrazzo 2016-04-27 17:04:01 +01:00 committed by Tom Christie
parent 4f16c54428
commit 88c80fe2e9
2 changed files with 18 additions and 0 deletions

View File

@ -992,6 +992,9 @@ class DecimalField(Field):
"""
Quantize the decimal value to the configured precision.
"""
if self.decimal_places is None:
return value
context = decimal.getcontext().copy()
context.prec = self.max_digits
return value.quantize(

View File

@ -894,6 +894,21 @@ class TestNoStringCoercionDecimalField(FieldValues):
)
class TestNoDecimalPlaces(FieldValues):
valid_inputs = {
'0.12345': Decimal('0.12345'),
}
invalid_inputs = {
'0.1234567': ['Ensure that there are no more than 6 digits in total.']
}
outputs = {
'1.2345': '1.2345',
'0': '0',
'1.1': '1.1',
}
field = serializers.DecimalField(max_digits=6, decimal_places=None)
# Date & time serializers...
class TestDateField(FieldValues):