使用Django Rest Framework Serializers时,推荐的数据转换方法是什么?例如:
输入:
{
"companyName" : "Acme , inc.",
"id": 2,
"parent": {
"id": 1
}
}
期望的输出:
{
"name" : "Acme , inc.",
"id": 2,
"parentId": 1
}
答案 0 :(得分:1)
使用序列化方法字段:
class Serializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField()
parentId = serializers.SerializerMethodField()
class Meta:
model =
fields = ('name', 'parentId')
def get_name(self, obj):
#write logic
def get_parentId(self, obj):
#write logic