Accept integers as min/max values of DecimalField (#9515)

* 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
This commit is contained in:
Bruno Alla 2024-09-07 12:07:28 +01:00 committed by GitHub
parent f593f5752c
commit 125ad42eb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 12 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.
* `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`.

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

@ -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):
@ -1260,14 +1260,14 @@ 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
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):