Currently, I have a ListAPIView that returns a list of object dictionaries:
[
{ id: 1, ...},
{ id: 2, ...},
...
]
I would like to change
I think you can implement the to_representation
function in your Serializer.
class MySerializer(serializers.Serializer):
id = serializers.ReadOnlyField()
field1 = serializers.ReadOnlyField()
field2 = serializers.ReadOnlyField()
def to_representation(self, data):
res = super(MySerializer, self).to_representation(data)
return {res['id']: res}
# or you can fetch the id by data directly
# return {str(data.id): res}