From 88c80fe2e901cf6607125b7bf57b4b2e084a4c7b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 27 Apr 2016 17:04:01 +0100 Subject: [PATCH] Fixed DecimalField arbitrary precision support (#4075) --- rest_framework/fields.py | 3 +++ tests/test_fields.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 2a08e09ff..5dcd546c0 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -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( diff --git a/tests/test_fields.py b/tests/test_fields.py index cefb45605..304de11ee 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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):