2

In DRF 3.1 I can do this to get paginated response but it seems like the get_paginated_response() is not available in 3.0. What would be the equivalent?

class NoteList(ListCreateAPIView):

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        paged_queryset = self.paginate_queryset(queryset)
        serializer = NoteSerializer(paged_queryset, many=True)
        return self.get_paginated_response(serializer.data)

1 Answer 1

2

It's PaginationSerializer, set the serializer class by:

'DEFAULT_PAGINATION_SERIALIZER_CLASS': 'YourCustomPaginationSerializer'

or

class MyView(generics.GenericAPIView):
    pagination_serializer_class = YourCustomPaginationSerializerClass

mixin.py

def list(self, request, *args, **kwargs):
    instance = self.filter_queryset(self.get_queryset())
    page = self.paginate_queryset(instance)
    if page is not None:
        serializer = self.get_pagination_serializer(page)
    else:
        serializer = self.get_serializer(instance, many=True)
    return Response(serializer.data)

generics.py

def get_pagination_serializer(self, page):
    """
    Return a serializer instance to use with paginated data.
    """
    class SerializerClass(self.pagination_serializer_class):
        class Meta:
            object_serializer_class = self.get_serializer_class()

    pagination_serializer_class = SerializerClass
    context = self.get_serializer_context()
    return pagination_serializer_class(instance=page, context=context)

If you are doing the migration stuffs, don't forget the pagination params in settings.py or View, has been changed to new paginator.

such as. PAGINATE_BY_PARAM -> page_size_query_param.

anyhow, I think the new paginator is much confortable than the previous one. see more detials on Pagination Docs

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.