Allow optional max_digits on DecimalField (#4377)

This commit is contained in:
Tom Christie 2016-08-10 14:39:26 +01:00 committed by GitHub
parent 2d43b17f9a
commit 48f3db36d6
2 changed files with 14 additions and 1 deletions

View File

@ -1016,7 +1016,8 @@ class DecimalField(Field):
return value
context = decimal.getcontext().copy()
context.prec = self.max_digits
if self.max_digits is not None:
context.prec = self.max_digits
return value.quantize(
decimal.Decimal('.1') ** self.decimal_places,
context=context

View File

@ -876,6 +876,18 @@ class TestMinMaxDecimalField(FieldValues):
)
class TestNoMaxDigitsDecimalField(FieldValues):
field = serializers.DecimalField(
max_value=100, min_value=0,
decimal_places=2, max_digits=None
)
valid_inputs = {
'10': Decimal('10.00')
}
invalid_inputs = {}
outputs = {}
class TestNoStringCoercionDecimalField(FieldValues):
"""
Output values for `DecimalField` with `coerce_to_string=False`.