Search code examples
djangodjango-rest-frameworkdjango-serializer

Customize Json Response in django rest framework


Given below is my serializer class. I have all fields in one model.I would like to change the representation of serializer data in custom format. Tried to_representation method of serializer but could not success.

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',........]

So in my view class defined and output corresponding to them also mentioned over here.

   class MyListAPIView(ListAPIView):
      def list(self,request):
          queryset=MyModel.objects.all()
          serializer=MyListSerilizer(queryset,many=True)
          return Response({'Records':serializer.data})

Output :---------->Corresponding to view is

  "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....

so my question is how would i achieve this json format. In short how do i customize my view class

     {
      "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

Solution

  • There is the method called to_representation in DRF. Reference:http://www.django-rest-framework.org/api-guide/fields/