diff options
| author | Tom Christie | 2014-09-22 15:34:06 +0100 | 
|---|---|---|
| committer | Tom Christie | 2014-09-22 15:34:06 +0100 | 
| commit | 4db23cae213decc3e8a8613ad5c76a545f8cfb1a (patch) | |
| tree | 462376cf2a472ea0251ad1c827d2a9a86dfc4e89 /rest_framework/fields.py | |
| parent | 249253a144ba4381581809fb3f27959c7bd6e577 (diff) | |
| download | django-rest-framework-4db23cae213decc3e8a8613ad5c76a545f8cfb1a.tar.bz2 | |
Tweaks to DecimalField
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 25 | 
1 files changed, 13 insertions, 12 deletions
| diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 5fb99a42..db7ceabb 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -521,20 +521,21 @@ class DecimalField(Field):          return value      def to_representation(self, value): -        if isinstance(value, decimal.Decimal): -            context = decimal.getcontext().copy() -            context.prec = self.max_digits -            quantized = value.quantize( -                decimal.Decimal('.1') ** self.decimal_places, -                context=context -            ) -            if not self.coerce_to_string: -                return quantized -            return '{0:f}'.format(quantized) +        if value in (None, ''): +            return None +        if not isinstance(value, decimal.Decimal): +            value = decimal.Decimal(value) + +        context = decimal.getcontext().copy() +        context.prec = self.max_digits +        quantized = value.quantize( +            decimal.Decimal('.1') ** self.decimal_places, +            context=context +        )          if not self.coerce_to_string: -            return value -        return '%.*f' % (self.max_decimal_places, value) +            return quantized +        return '{0:f}'.format(quantized)  # Date & time fields... | 
