Skip to content

Conversation

HoodyH
Copy link

@HoodyH HoodyH commented Sep 5, 2025

Fixing Bigint coerce to string missing

Creates a new type BigIntegerField to map django.models.BigIntegerField and solve the problem

Fix #9733

Copy link
Collaborator

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@HoodyH
Copy link
Author

HoodyH commented Sep 22, 2025

Any news?

@auvipy auvipy added this to the 3.17 milestone Sep 22, 2025

* `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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo (BigInterger)


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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add (by default) after "class", as it can be overridden

)
from rest_framework.exceptions import ErrorDetail, ValidationError
from rest_framework.fields import get_error_detail
from rest_framework.fields import get_error_detail, BigIntegerField
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the addition to the block starting in line 55


def test_force_coerce_to_string(self):
field = serializers.BigIntegerField(coerce_to_string=True)
assert isinstance(field.to_representation(int('1')), str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why int('1')?

And why only test the type, not the value itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bigint coerce to string missing

3 participants