mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Fixed DecimalField arbitrary precision support (#4075)
This commit is contained in:
parent
4f16c54428
commit
88c80fe2e9
|
@ -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(
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user