Skip to content

Commit 88c80fe

Browse files
dvarrazzotomchristie
authored andcommitted
Fixed DecimalField arbitrary precision support (#4075)
1 parent 4f16c54 commit 88c80fe

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

rest_framework/fields.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,9 @@ def quantize(self, value):
992992
"""
993993
Quantize the decimal value to the configured precision.
994994
"""
995+
if self.decimal_places is None:
996+
return value
997+
995998
context = decimal.getcontext().copy()
996999
context.prec = self.max_digits
9971000
return value.quantize(

tests/test_fields.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,21 @@ class TestNoStringCoercionDecimalField(FieldValues):
894894
)
895895

896896

897+
class TestNoDecimalPlaces(FieldValues):
898+
valid_inputs = {
899+
'0.12345': Decimal('0.12345'),
900+
}
901+
invalid_inputs = {
902+
'0.1234567': ['Ensure that there are no more than 6 digits in total.']
903+
}
904+
outputs = {
905+
'1.2345': '1.2345',
906+
'0': '0',
907+
'1.1': '1.1',
908+
}
909+
field = serializers.DecimalField(max_digits=6, decimal_places=None)
910+
911+
897912
# Date & time serializers...
898913

899914
class TestDateField(FieldValues):

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