Home > OS >  How to conditionally enable and disable Cursor Pagination on Django Rest Framework?
How to conditionally enable and disable Cursor Pagination on Django Rest Framework?

Time:03-14

I have a specific usecase where I need to make available same queryset endpoint for the frontend with and without pagination. I need to use CursorPagination and currently I'm just setting pagination_class = CursorPagination on my *ViewSet class.

Is there an easy way to achieve this requirement?

CodePudding user response:

You can use Paginator class as following example:

from django.core.paginator import Paginator

YourClass():
#your code
paginator = Paginator(user_list, 10)
    try:
        users = paginator.page(page)
    except PageNotAnInteger:
        users = paginator.page(1)

CodePudding user response:

Found a solution. You can override page_size_query_param variable on CursorPagination class. That way you can allow the frontend to set how many records it will get per page. So the frontend can use a very large number for the page size query parameter to get all the data. Not sure if this approach has some inherent problems though.

  • Related