Skip to content

Commit 48f3db3

Browse files
authored
Allow optional max_digits on DecimalField (#4377)
1 parent 2d43b17 commit 48f3db3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

rest_framework/fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,8 @@ def quantize(self, value):
10161016
return value
10171017

10181018
context = decimal.getcontext().copy()
1019-
context.prec = self.max_digits
1019+
if self.max_digits is not None:
1020+
context.prec = self.max_digits
10201021
return value.quantize(
10211022
decimal.Decimal('.1') ** self.decimal_places,
10221023
context=context

tests/test_fields.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,18 @@ class TestMinMaxDecimalField(FieldValues):
876876
)
877877

878878

879+
class TestNoMaxDigitsDecimalField(FieldValues):
880+
field = serializers.DecimalField(
881+
max_value=100, min_value=0,
882+
decimal_places=2, max_digits=None
883+
)
884+
valid_inputs = {
885+
'10': Decimal('10.00')
886+
}
887+
invalid_inputs = {}
888+
outputs = {}
889+
890+
879891
class TestNoStringCoercionDecimalField(FieldValues):
880892
"""
881893
Output values for `DecimalField` with `coerce_to_string=False`.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy