Skip to content

DecimalField fails when max_digits=None #4372

@terramars

Description

@terramars

Steps to reproduce

  1. Create a DecimalField inside a serializer with max_value, min_value, and decimal_places set, but set max_digits = None. EG: serializers.DecimalField(max_value=100, min_value=0, decimal_places=10, max_digits=None)
  2. call serializer.is_valid()
  3. observe traceback similar to the following :

Traceback (most recent call last):
File "/api/views.py", line 263, in monitor
serializer.is_valid()
File "
/env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 213, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 407, in run_validation
value = self.to_internal_value(data)
File "
/env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 437, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/env/local/lib/python2.7/site-packages/rest_framework/fields.py", line 488, in run_validation
value = self.to_internal_value(data)
File "
/env/local/lib/python2.7/site-packages/rest_framework/fields.py", line 959, in to_internal_value
return self.quantize(self.validate_precision(value))
File "~/env/local/lib/python2.7/site-packages/rest_framework/fields.py", line 1022, in quantize
context=context
File "/usr/lib/python2.7/decimal.py", line 2455, in quantize
if not (context.Etiny() <= exp._exp <= context.Emax):
File "/usr/lib/python2.7/decimal.py", line 3898, in Etiny
return int(self.Emin - self.prec + 1)
TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'

Expected behavior

is_valid returns true if the value is between min and max, without concerning itself with the number of digits / precision of the number, OR do not accept None as a valid value for max_digits.

Actual behavior

an exception is raised, which is difficult to track down and counter intuitive.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions