From cd40815f8488cbc05815750405281ab7677c82d5 Mon Sep 17 00:00:00 2001 From: kiyoqoko Date: Wed, 6 Jul 2016 09:42:00 +0200 Subject: [PATCH] localize forces coerce_to_string to True --- docs/api-guide/fields.md | 4 ++-- rest_framework/fields.py | 6 ++++-- tests/test_fields.py | 4 ++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 581734328..f95608afb 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -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. - `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. - `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 diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 485289193..46d7ed09b 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -904,6 +904,8 @@ class DecimalField(Field): self.localize = localize if coerce_to_string is not None: self.coerce_to_string = coerce_to_string + if self.localize: + self.coerce_to_string = True self.max_value = max_value self.min_value = min_value @@ -928,11 +930,11 @@ class DecimalField(Field): instance. """ + data = smart_text(data).strip() + if self.localize: data = sanitize_separators(data) - data = smart_text(data).strip() - if len(data) > self.MAX_STRING_LENGTH: self.fail('max_string_length') diff --git a/tests/test_fields.py b/tests/test_fields.py index 652733225..1149cc4b3 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -906,6 +906,10 @@ class TestLocalizedDecimalField(TestCase): field = serializers.DecimalField(max_digits=2, decimal_places=1, localize=True) 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): valid_inputs = {