mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
localize forces coerce_to_string to True
This commit is contained in:
parent
2bcf2eb549
commit
cd40815f84
|
@ -261,10 +261,10 @@ Corresponds to `django.db.models.fields.DecimalField`.
|
||||||
|
|
||||||
- `max_digits` The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places.
|
- `max_digits` The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places.
|
||||||
- `decimal_places` The number of decimal places to store with the number.
|
- `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.
|
- `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.
|
- `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.
|
- `min_value` Validate that the number provided is no less than this value.
|
||||||
- `localize` Set to `True` to enable localization of input and output based on the current locale. Defaults to `False`. Note that data formatting is enabled if you have set `USE_L10N=True` in your settings file.
|
- `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.
|
||||||
|
|
||||||
#### Example usage
|
#### Example usage
|
||||||
|
|
||||||
|
|
|
@ -904,6 +904,8 @@ class DecimalField(Field):
|
||||||
self.localize = localize
|
self.localize = localize
|
||||||
if coerce_to_string is not None:
|
if coerce_to_string is not None:
|
||||||
self.coerce_to_string = coerce_to_string
|
self.coerce_to_string = coerce_to_string
|
||||||
|
if self.localize:
|
||||||
|
self.coerce_to_string = True
|
||||||
|
|
||||||
self.max_value = max_value
|
self.max_value = max_value
|
||||||
self.min_value = min_value
|
self.min_value = min_value
|
||||||
|
@ -928,11 +930,11 @@ class DecimalField(Field):
|
||||||
instance.
|
instance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
data = smart_text(data).strip()
|
||||||
|
|
||||||
if self.localize:
|
if self.localize:
|
||||||
data = sanitize_separators(data)
|
data = sanitize_separators(data)
|
||||||
|
|
||||||
data = smart_text(data).strip()
|
|
||||||
|
|
||||||
if len(data) > self.MAX_STRING_LENGTH:
|
if len(data) > self.MAX_STRING_LENGTH:
|
||||||
self.fail('max_string_length')
|
self.fail('max_string_length')
|
||||||
|
|
||||||
|
|
|
@ -906,6 +906,10 @@ class TestLocalizedDecimalField(TestCase):
|
||||||
field = serializers.DecimalField(max_digits=2, decimal_places=1, localize=True)
|
field = serializers.DecimalField(max_digits=2, decimal_places=1, localize=True)
|
||||||
self.assertEqual(field.to_representation(Decimal('1.1')), '1,1')
|
self.assertEqual(field.to_representation(Decimal('1.1')), '1,1')
|
||||||
|
|
||||||
|
def test_localize_forces_coerce_to_string(self):
|
||||||
|
field = serializers.DecimalField(max_digits=2, decimal_places=1, coerce_to_string=False, localize=True)
|
||||||
|
self.assertTrue(isinstance(field.to_representation(Decimal('1.1')), six.string_types))
|
||||||
|
|
||||||
|
|
||||||
class TestNoDecimalPlaces(FieldValues):
|
class TestNoDecimalPlaces(FieldValues):
|
||||||
valid_inputs = {
|
valid_inputs = {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user