以下是我的序列化程序类。我在一个模型中有所有字段。我想以自定义格式更改序列化数据的表示。试过序列化器的to_representation方法,但无法成功。
class MyListSerilizer(ModelSerializer):
class Meta:
model=MyModel
fields=['Name','Address_ID','Portal','Address','City','DisplayOrderDateTime','Email','Order_ID','Order_Code','Item_Code','DispatchedOn','Payment_Mode','Shipping_Charge','ShippingMethodCode','ShippingMethodCharges','CashOnDeliveryCharges','CurrencyCode','BillingAddress','GiftWrapCharges','SaleOrderItemCode','Shipping_ref','Cancellable','OnHold','Quantity','Invoice_No''Portal',........]
所以在我的视图中定义了类,并且在这里也提到了与它们对应的输出。
class MyListAPIView(ListAPIView):
def list(self,request):
queryset=MyModel.objects.all()
serializer=MyListSerilizer(queryset,many=True)
return Response({'Records':serializer.data})
输出:---------->对应于视图
"Records": [
{
"Name": "abc",
"Address_ID": "6819319",
"Portal": "amksl",
"Address": "",
"City": "absjsls",
"DisplayOrderDateTime": null,
"Email": "abcd@gmail.com",
"Order_ID": "",
"Order_Code": "",
"Item_Code": "",
"DispatchedOn": "",
"Payment_Mode": ""
},
{
"Name": "abc",
"Address_ID": "6819319",
"Portal": "amksl",
"Address": "",
"City": "absjsls",
"DisplayOrderDateTime": null,
"Email": "abcd@gmail.com",
"Order_ID": "",
"Order_Code": "",
"Item_Code": "",
"DispatchedOn": "",
"Payment_Mode": ""
},
so on....
所以我的问题是如何实现这种json格式。简而言之,我如何自定义我的视图类
{
"identifiers":{
"email":"abcd@gmai.com",
"phone":"123664"
},
"activity_type": "purchase",
"timestamp": "UNIX TIMESTAMP",
"products": [{
"brandName": "abc",
"id": "1",
"sku": "abcd",
"name": "mnis",
"price": 12.9,
"discount": "",
"quantity": "",
"currency": ""
}]
"cart_info":{
"total":"",
"revenue":"",
"currency":""
},
"Order_info":{
"total":"2121",
.
.
.
}
},
{
"identifiers":{
"email":"abcd@gmai.com",
"phone":"123664"
},
"activity_type": "purchase",
"timestamp": "UNIX TIMESTAMP",
"products": [{
"brandName": "abc",
"id": "1",
"sku": "abcd",
"name": "mnis",
"price": 12.9,
"discount": "",
"quantity": "",
"currency": ""
}]
"cart_info":{
"total":"",
"revenue":"",...so on
答案 0 :(得分:4)
覆盖序列化程序类
中的 to_representation 方法class MyListSerilizer(ModelSerializer):
class Meta:
model=MyModel
fields=['Name','Address_ID','Portal','Address','City','DisplayOrderDateTime','Email','Order_ID','Order_Code','Item_Code','DispatchedOn','Payment_Mode','Shipping_Charge','ShippingMethodCode','ShippingMethodCharges','CashOnDeliveryCharges','CurrencyCode','BillingAddress','GiftWrapCharges','SaleOrderItemCode','Shipping_ref','Cancellable','OnHold','Quantity','Invoice_No''Portal',........]
def to_representation(self, instance):
# instance is the model object. create the custom json format by accessing instance attributes normaly and return it
identifiers = dict()
identifiers['email'] = instance.Email
identifiers['phone'] = instance.phone
representation = {
'identifiers': identifiers,
'activity_type': instance.xxxx,
'timestamp': instance.xxxxx,
.
.
. -> your custom data
}
return representation
无论何时调用serializer.data,都会返回此表示字典
答案 1 :(得分:1)
DRF中有一种名为to_representation
的方法。
参考:http://www.django-rest-framework.org/api-guide/fields/