0

views.py

def list(self, request, *args, **kwargs):
        queryset= User.objects.all().values_list('name','designation')
        queryset=[{'name':i[0],'designation':i[1]} for i in queryset]
        serializer=getuserserializer(queryset,many=True)
        return Response(serializer.data)

serializer.py

class getuserserializer(serializers.Serializer):
    name=serializers.CharField()
    designation=serializers.CharField()

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 100
}

I have looked through the solutions, which suggested to change settings.py as above. Yet i am getting output like :

{
  "result": [
    {
      "name": "Shubham Kumar",
      "designation": "SE"
    }
  ]
}

How to convert it to :

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [ 
        {
          "name": "Shubham Kumar",
          "designation": "SE"
        }
      ]
 }
Shubham Kumar
  • 105
  • 1
  • 1
  • 8

1 Answers1

2

Your problem is that you are overriding the list method of your view. And your view, given the code, doesn't make a lot of sense - you're filtering by an id, accessing two values of a tuple/array, serializing that result and returning it.

If you check the ListModelMixin class you can understand how the pagination class is used:

def list(self, request, *args, **kwargs):
    queryset = self.filter_queryset(self.get_queryset())

    page = self.paginate_queryset(queryset)
    if page is not None:
        serializer = self.get_serializer(page, many=True)
        return self.get_paginated_response(serializer.data)

    serializer = self.get_serializer(queryset, many=True)
    return Response(serializer.data)
henriquesalvaro
  • 1,232
  • 1
  • 8
  • 14