Skip to content

Commit

Permalink
Accept integers as min/max values of DecimalField (#9515)
Browse files Browse the repository at this point in the history
* Use Decimal for min/max values of DecimalField in tests

* Update docs to mention that min/max values should be Decimal objects

* Accept integer values for DecimalField min and max values

* Update expected error messages in tests

* Update expected warning message in tests
  • Loading branch information
browniebroke authored Sep 7, 2024
1 parent f593f57 commit 125ad42
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/api-guide/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ Corresponds to `django.db.models.fields.DecimalField`.
* `max_digits` The maximum number of digits allowed in the number. It must be either `None` or an integer greater than or equal to `decimal_places`.
* `decimal_places` The number of decimal places to store with the number.
* `coerce_to_string` Set to `True` if string values should be returned for the representation, or `False` if `Decimal` objects should be returned. Defaults to the same value as the `COERCE_DECIMAL_TO_STRING` settings key, which will be `True` unless overridden. If `Decimal` objects are returned by the serializer, then the final output format will be determined by the renderer. Note that setting `localize` will force the value to `True`.
* `max_value` Validate that the number provided is no greater than this value.
* `min_value` Validate that the number provided is no less than this value.
* `max_value` Validate that the number provided is no greater than this value. Should be an integer or `Decimal` object.
* `min_value` Validate that the number provided is no less than this value. Should be an integer or `Decimal` object.
* `localize` Set to `True` to enable localization of input and output based on the current locale. This will also force `coerce_to_string` to `True`. Defaults to `False`. Note that data formatting is enabled if you have set `USE_L10N=True` in your settings file.
* `rounding` Sets the rounding mode used when quantizing to the configured precision. Valid values are [`decimal` module rounding modes][python-decimal-rounding-modes]. Defaults to `None`.
* `normalize_output` Will normalize the decimal value when serialized. This will strip all trailing zeroes and change the value's precision to the minimum required precision to be able to represent the value without losing data. Defaults to `False`.
Expand Down
8 changes: 4 additions & 4 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,10 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
self.max_value = max_value
self.min_value = min_value

if self.max_value is not None and not isinstance(self.max_value, decimal.Decimal):
warnings.warn("max_value should be a Decimal instance.")
if self.min_value is not None and not isinstance(self.min_value, decimal.Decimal):
warnings.warn("min_value should be a Decimal instance.")
if self.max_value is not None and not isinstance(self.max_value, (int, decimal.Decimal)):
warnings.warn("max_value should be an integer or Decimal instance.")
if self.min_value is not None and not isinstance(self.min_value, (int, decimal.Decimal)):
warnings.warn("min_value should be an integer or Decimal instance.")

if self.max_digits is not None and self.decimal_places is not None:
self.max_whole_digits = self.max_digits - self.decimal_places
Expand Down
12 changes: 6 additions & 6 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,13 +1245,13 @@ class TestMinMaxDecimalField(FieldValues):
'20.0': Decimal('20.0'),
}
invalid_inputs = {
'9.9': ['Ensure this value is greater than or equal to 10.'],
'20.1': ['Ensure this value is less than or equal to 20.'],
'9.9': ['Ensure this value is greater than or equal to 10.0.'],
'20.1': ['Ensure this value is less than or equal to 20.0.'],
}
outputs = {}
field = serializers.DecimalField(
max_digits=3, decimal_places=1,
min_value=10, max_value=20
min_value=10.0, max_value=20.0
)

def test_warning_when_not_decimal_types(self, caplog):
Expand All @@ -1260,14 +1260,14 @@ def test_warning_when_not_decimal_types(self, caplog):

serializers.DecimalField(
max_digits=3, decimal_places=1,
min_value=10, max_value=20
min_value=10.0, max_value=20.0
)

assert len(w) == 2
assert all(issubclass(i.category, UserWarning) for i in w)

assert 'max_value should be a Decimal instance' in str(w[0].message)
assert 'min_value should be a Decimal instance' in str(w[1].message)
assert 'max_value should be an integer or Decimal instance' in str(w[0].message)
assert 'min_value should be an integer or Decimal instance' in str(w[1].message)


class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
Expand Down

0 comments on commit 125ad42

Please sign in to comment.