[Answered ]-Change Django Rest Framework serializers output structure?

2đź‘Ť

You need to override the to_representation() method of your serializer class. It is mention in brief in this official documentation link:
http://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior

Other option is overriding the get and post methods of your class APIView or it’s inherited class

👤Swakeert Jain

0đź‘Ť

You can’t store key/value pairs in list, ['key': {}] won’t work, you should access to item by index instead, like in your representation.
Array returned because of many=True which means you put many objects in serializer and it wait sequence from you.

0đź‘Ť

I can’t see the ORM query that resulted in this output, but Your query is returning multiple items that match your criteria. Default behaviour of any frameworks, let alone DRF, is to put them inside an array and return them as an “Array of objects” as in your result.

👤Ashif Shereef

Leave a comment