尝试对一个大型查询集分页,这样即使数据已添加到数据库中,我也可以返回到以前所在的位置。
目前我有一个分页类:
from rest_framework.pagination import CursorPagination
class MessageCursorPagination(CursorPagination):
page_size = 25
ordering = '-date'
在我看来,我有:
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
在测试是否设置正确时,我得到了下一个链接的预期结果,上一个链接为空。
进入下一个链接后,我得到一个新的下一个链接、下一组结果和上一个链接。
当我继续下一个链接时,我会得到与以前相同的前一个链接,但会得到下一个、下一个链接和下一组数据。
无论我去下一个链接多少次,下一个链接前一个链接保持不变。
为什么以前的链接没有更新?
--更新--
我的问题似乎是因为我在同一天收到了很多信息。按日期排序它试图返回到当前光标之前的日期。我怎样才能按日期排序,但是像使用ID那样使用光标分页来逐步浏览列表?