Skip to content

Commit

Permalink
Allow optional max_digits on DecimalField (#4377)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Aug 10, 2016
1 parent 2d43b17 commit 48f3db3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rest_framework/fields.py
Expand Up @@ -1016,7 +1016,8 @@ def quantize(self, value):
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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_fields.py
Expand Up @@ -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`.
Expand Down

0 comments on commit 48f3db3

Please sign in to comment.