diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 4c1fcb968..0dbb2b870 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -279,7 +279,7 @@ Corresponds to `django.db.models.fields.BigIntegerField`. * `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. -* `coerce_to_string` Set to `True` if string values should be returned for the representation, or `False` if `BigInteger` objects should be returned. Defaults to the same value as the `COERCE_BIGINT_TO_STRING` settings key, which will be `True` unless overridden. If `BigInterger` 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 `BigInteger` objects should be returned. Defaults to the same value as the `COERCE_BIGINT_TO_STRING` settings key, which will be `True` unless overridden. If `BigInteger` objects are returned by the serializer, then the final output format will be determined by the renderer. ## FloatField diff --git a/docs/api-guide/settings.md b/docs/api-guide/settings.md index 79eabf3ce..390dbd911 100644 --- a/docs/api-guide/settings.md +++ b/docs/api-guide/settings.md @@ -375,7 +375,7 @@ Default: `True` When returning biginteger objects in API representations that do not support numbers up to 2^64, it is best to return the value as a string. This avoids the loss of precision that occurs with biginteger implementations. -When set to `True`, the serializer `BigIntegerField` class will return strings instead of `BigInteger` objects. When set to `False`, serializers will return `BigInteger` objects, which the default JSON encoder will return as numbers. +When set to `True`, the serializer `BigIntegerField` class (by default) will return strings instead of `BigInteger` objects. When set to `False`, serializers will return `BigInteger` objects, which the default JSON encoder will return as numbers. Default: `False` diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5709c7141..9b2a813fd 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -53,10 +53,10 @@ from rest_framework.validators import ( # This helps keep the separation between model fields, form fields, and # serializer fields more explicit. from rest_framework.fields import ( # NOQA # isort:skip - BooleanField, CharField, ChoiceField, DateField, DateTimeField, DecimalField, + BigIntegerField, BooleanField, CharField, ChoiceField, DateField, DateTimeField, DecimalField, DictField, DurationField, EmailField, Field, FileField, FilePathField, FloatField, HiddenField, HStoreField, IPAddressField, ImageField, IntegerField, JSONField, - ListField, ModelField, MultipleChoiceField, ReadOnlyField, BigIntegerField, + ListField, ModelField, MultipleChoiceField, ReadOnlyField, RegexField, SerializerMethodField, SlugField, TimeField, URLField, UUIDField, ) from rest_framework.relations import ( # NOQA # isort:skip diff --git a/tests/test_fields.py b/tests/test_fields.py index 61563130a..80f3ed1e8 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1158,7 +1158,9 @@ class TestCoercionBigIntegerField(TestCase): def test_force_coerce_to_string(self): field = serializers.BigIntegerField(coerce_to_string=True) - assert isinstance(field.to_representation(int('1')), str) + value = field.to_representation(1) + assert isinstance(value, str) + assert value == "1" class TestFloatField(FieldValues):