[Django]-How can I add additional top level JSON fields using the ModelSerializer

3👍

You can do this by overriding the list method of the ListAPIView and then adding this new field to the response:

from rest_framework.response import Response

def list(self, request, *args, **kwargs):
    queryset = self.get_queryset()
    serializer = self.get_serializer(queryset, many=True)

    response_data = {'custom_field': 'custom value',
                     'result': serializer.data}

    return Response(response_data)

Leave a comment