Accept integer values for DecimalField min and max values

This commit is contained in:
Bruno Alla 2024-09-06 16:40:10 +01:00
parent b8c646ac86
commit 9fef0f6926
No known key found for this signature in database
4 changed files with 11 additions and 16 deletions

View File

@ -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. Should be a `Decimal` object.
* `min_value` Validate that the number provided is no less than this value. Should be a `Decimal` object.
* `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`.

View File

@ -986,10 +986,10 @@ class DecimalField(Field):
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

View File

@ -2,7 +2,6 @@
This test "app" exists to ensure that parts of Django REST Framework can be
imported/invoked before Django itself has been fully initialized.
"""
from decimal import Decimal
from rest_framework import compat, serializers # noqa
@ -12,8 +11,6 @@ class ExampleSerializer(serializers.Serializer):
charfield = serializers.CharField(min_length=1, max_length=2)
integerfield = serializers.IntegerField(min_value=1, max_value=2)
floatfield = serializers.FloatField(min_value=1, max_value=2)
decimalfield = serializers.DecimalField(
max_digits=10, decimal_places=1, min_value=Decimal(1), max_value=Decimal(2)
)
decimalfield = serializers.DecimalField(max_digits=10, decimal_places=1, min_value=1, max_value=2)
durationfield = serializers.DurationField(min_value=1, max_value=2)
listfield = serializers.ListField(min_length=1, max_length=2)

View File

@ -1251,7 +1251,7 @@ class TestMinMaxDecimalField(FieldValues):
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):
@ -1260,7 +1260,7 @@ class TestMinMaxDecimalField(FieldValues):
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
@ -1291,14 +1291,12 @@ class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
outputs = {
None: '',
}
field = serializers.DecimalField(
max_digits=3, decimal_places=1, allow_null=True, min_value=Decimal(0), max_value=Decimal(10)
)
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True, min_value=0, max_value=10)
class TestNoMaxDigitsDecimalField(FieldValues):
field = serializers.DecimalField(
max_value=Decimal(100), min_value=Decimal(0),
max_value=100, min_value=0,
decimal_places=2, max_digits=None
)
valid_inputs = {