Skip to content

Commit 9fef0f6

Browse files
committed
Accept integer values for DecimalField min and max values
1 parent b8c646a commit 9fef0f6

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

docs/api-guide/fields.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ Corresponds to `django.db.models.fields.DecimalField`.
291291
* `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`.
292292
* `decimal_places` The number of decimal places to store with the number.
293293
* `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`.
294-
* `max_value` Validate that the number provided is no greater than this value. Should be a `Decimal` object.
295-
* `min_value` Validate that the number provided is no less than this value. Should be a `Decimal` object.
294+
* `max_value` Validate that the number provided is no greater than this value. Should be an integer or `Decimal` object.
295+
* `min_value` Validate that the number provided is no less than this value. Should be an integer or `Decimal` object.
296296
* `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.
297297
* `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`.
298298
* `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`.

rest_framework/fields.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,10 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
986986
self.max_value = max_value
987987
self.min_value = min_value
988988

989-
if self.max_value is not None and not isinstance(self.max_value, decimal.Decimal):
990-
warnings.warn("max_value should be a Decimal instance.")
991-
if self.min_value is not None and not isinstance(self.min_value, decimal.Decimal):
992-
warnings.warn("min_value should be a Decimal instance.")
989+
if self.max_value is not None and not isinstance(self.max_value, (int, decimal.Decimal)):
990+
warnings.warn("max_value should be an integer or Decimal instance.")
991+
if self.min_value is not None and not isinstance(self.min_value, (int, decimal.Decimal)):
992+
warnings.warn("min_value should be an integer or Decimal instance.")
993993

994994
if self.max_digits is not None and self.decimal_places is not None:
995995
self.max_whole_digits = self.max_digits - self.decimal_places

tests/importable/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
This test "app" exists to ensure that parts of Django REST Framework can be
33
imported/invoked before Django itself has been fully initialized.
44
"""
5-
from decimal import Decimal
65

76
from rest_framework import compat, serializers # noqa
87

@@ -12,8 +11,6 @@ class ExampleSerializer(serializers.Serializer):
1211
charfield = serializers.CharField(min_length=1, max_length=2)
1312
integerfield = serializers.IntegerField(min_value=1, max_value=2)
1413
floatfield = serializers.FloatField(min_value=1, max_value=2)
15-
decimalfield = serializers.DecimalField(
16-
max_digits=10, decimal_places=1, min_value=Decimal(1), max_value=Decimal(2)
17-
)
14+
decimalfield = serializers.DecimalField(max_digits=10, decimal_places=1, min_value=1, max_value=2)
1815
durationfield = serializers.DurationField(min_value=1, max_value=2)
1916
listfield = serializers.ListField(min_length=1, max_length=2)

tests/test_fields.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ class TestMinMaxDecimalField(FieldValues):
12511251
outputs = {}
12521252
field = serializers.DecimalField(
12531253
max_digits=3, decimal_places=1,
1254-
min_value=10, max_value=20
1254+
min_value=10.0, max_value=20.0
12551255
)
12561256

12571257
def test_warning_when_not_decimal_types(self, caplog):
@@ -1260,7 +1260,7 @@ def test_warning_when_not_decimal_types(self, caplog):
12601260

12611261
serializers.DecimalField(
12621262
max_digits=3, decimal_places=1,
1263-
min_value=10, max_value=20
1263+
min_value=10.0, max_value=20.0
12641264
)
12651265

12661266
assert len(w) == 2
@@ -1291,14 +1291,12 @@ class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
12911291
outputs = {
12921292
None: '',
12931293
}
1294-
field = serializers.DecimalField(
1295-
max_digits=3, decimal_places=1, allow_null=True, min_value=Decimal(0), max_value=Decimal(10)
1296-
)
1294+
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True, min_value=0, max_value=10)
12971295

12981296

12991297
class TestNoMaxDigitsDecimalField(FieldValues):
13001298
field = serializers.DecimalField(
1301-
max_value=Decimal(100), min_value=Decimal(0),
1299+
max_value=100, min_value=0,
13021300
decimal_places=2, max_digits=None
13031301
)
13041302
valid_inputs = {

0 commit comments

Comments
 (0)