Trying to paginate a large queryset so I can return to the same position I was in previously even if data has been added to the database.
Currently I have as my pagination class:
from rest_framework.pagination import CursorPagination
class MessageCursorPagination(CursorPagination):
page_size = 25
ordering = '-date'
In my View I have:
from rest_framework.generics import GenericAPIView
from rest_framework.authentication import TokenAuthentication, BasicAuthentication
class MessageViewSet(GenericAPIView):
permission_classes = (IsAuthenticated, )
authentication_classes = (TokenAuthentication,)
pagination_class = pagination.MessageCursorPagination
serializer_class = serializers.MessageSerializer
def get(self, request, **kwargs):
account_id = kwargs.get('account_id', None)
messages = models.Message.objects.filter(
account=account_id)
paginated_messages = self.paginate_queryset(messages)
results = self.serializer_class(paginated_messages, many=True).data
response = self.get_paginated_response(results)
return response
While testing to see if I'd set it up right, I got the results I was expecting with a next link and a null for the previous link. After going to the next link I get a new next link, the next set of results, and a previous link. When I continue to the next link I get the same previous link as before, but with the next, next link and the next set of data. No matter how many times I go to the next, next link the previous link remains the same.
Why doesn't the previous link update?
-- Update --
It looks like the cause to my issue is that I have a lot of messages on the same date. Ordering by date it tries to step back to the date before the current cursor. How can I order by date but step through the list using the cursor pagination like I would with ids?
I ended up creating a property on the model that was the date followed by the id of the model.
I then did an order by on that property and it fixed the issue.
From the Documentation
Proper usage of cursor pagination should have an ordering field that satisfies the following: