[Django]-Named JSON array in Django REST Framework

0👍

Take a look at the list() method here.

Assuming you’re not using paginated data (which you question implys) it’s essentially just doing this:

def list(self, request, *args, **kwargs):
    self.object_list = self.filter_queryset(self.get_queryset())
    serializer = self.get_serializer(self.object_list, many=True)
    return Response(serializer.data)

So, you just need to override that to use your own custom style instead…

def list(self, request, *args, **kwargs):
    self.object_list = self.filter_queryset(self.get_queryset())
    serializer = self.get_serializer(self.object_list, many=True)
    return Response({'results': serializer.data})

Leave a comment