-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Bigint coerce to string #9775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Bigint coerce to string #9775
Conversation
… can have string api representation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good
Any news? |
|
||
* `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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
rest_framework/serializers.py
Outdated
) | ||
from rest_framework.exceptions import ErrorDetail, ValidationError | ||
from rest_framework.fields import get_error_detail | ||
from rest_framework.fields import get_error_detail, BigIntegerField |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
Fixing Bigint coerce to string missing
Creates a new type
BigIntegerField
to mapdjango.models.BigIntegerField
and solve the problemFix #9733