Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF Cursor Pagination example

I'm trying to setup CursorPagination with DRF for a list of transaction records (ordered by creation date). I can't figure out how to do an initial request because I don't know the cursor yet at that stage. Surprisingly, I can't find an example of this.

Also, is there a way to set the page size per request with CursorPagination, the PageNumberPagination have page_size_query_param and max_page_size and they aren't there for CursorPagination.

Here's what I have so far:

class RecordPagination(pagination.CursorPagination):
    page_size = 10

class RecordsOverview(generics.ListAPIView):
    serializer_class = RecordSerializer
    logging_methods = ['GET']

    queryset = Record.objects.all()
    pagination_class = RecordPagination

    # Note: this is my way to dynamically set the page size, 
    # it is totally hacky, so I'm open to suggestions 
    # is_number method is left out for brevity
    def get(self, request, *args, **kwargs):
        page_size = request.GET.get('page_size', '')

        if self.is_number(page_size) and int(page_size) > 0:
            self.paginator.page_size = int(page_size)

        return self.list(request, *args, **kwargs)

Then in my test I do a GET request:

response = self.client.get(GET_RECORDS_URL, data={'page_size': 2})

I get back a 'next' url that looks something like this:

http://testserver/api/v1/records/?cursor=cj0xJnA9MjAxNy0wOS0yMCsxNCUzQTM0JTNBNDkuNjUxMDU4JTJCMDAlM0EwMA%3D%3D&page_size=2

If I do get(next_url) I will get the next records OK, and this time I don't have to pass data={'page_size': 2}.

Please, let me know if I can do all of this in a cleaner and more consistent way.

like image 558
foobar Avatar asked Dec 14 '22 20:12

foobar


1 Answers

This is how I use CursorPagination without any complications:

from rest_framework.pagination import CursorPagination

class CursorSetPagination(CursorPagination):
    page_size = 5
    page_size_query_param = 'page_size'
    ordering = '-timestamp' # '-created' is default

class MyListAPIView(generics.ListAPIView):
    queryset = MyObject.objects.all()
    serializer_class = MySerializer
    pagination_class = CursorSetPagination
like image 56
DevB2F Avatar answered Feb 05 '23 04:02

DevB2F