Last active
October 16, 2020 02:07
-
-
Save akkuman/ba2db31d784535397e31d99002d1e2e7 to your computer and use it in GitHub Desktop.
drf框架里面的针对bigint字段的序列化字段,主要原因是前端js对于bigint类型字段会有精度缺失
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
from rest_framework import serializers | |
from django.db import models | |
from rest_framework.fields import IntegerField | |
class BigIntegerField(IntegerField): | |
def to_representation(self, value): | |
return str(value) | |
class BigIntegerPKRelField(serializers.PrimaryKeyRelatedField): | |
def __init__(self, **kwargs): | |
kwargs['pk_field'] = BigIntegerField() | |
super().__init__(**kwargs) | |
class MyModelSerializer(serializers.ModelSerializer): | |
serializer_field_mapping = copy.deepcopy(serializers.ModelSerializer.serializer_field_mapping) | |
serializer_field_mapping[models.BigIntegerField] = BigIntegerField | |
serializer_related_field = BigIntegerPKRelField |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment