1

使用 DRF 的 CursorPagination 时出现错误 IndexError: tuple index out of range。我的代码-

class CursorSetPagination(CursorPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    ordering = '-created_at' 


class WalletCreditViewset(viewsets.ReadOnlyModelViewSet):

    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]
    pagination_class = CursorSetPagination 
    serializer_class =  WalletCreditSerializer

    def get_queryset(self):
        queryset = PaymentOrder.objects.filter(user=self.request.user)

        return queryset

当特定用户的数据库表中的条目与 page_size 的值交叉时,就会出现错误。

例如,如果某个用户有 5 个付款订单,则不会出现错误,但是当同一用户跨 10 个付款订单时,则会发生此错误。

4

1 回答 1

0

我认为您必须在查询集中订购数据,而不是在分页中。
例如:

class CursorSetPagination(CursorPagination):
    page_size = 10
    page_size_query_param = 'page_size'


class WalletCreditViewset(viewsets.ReadOnlyModelViewSet):

    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]
    pagination_class = CursorSetPagination 
    serializer_class =  WalletCreditSerializer

    def get_queryset(self):
        queryset = PaymentOrder.objects.filter(user=self.request.user).order_by("-created_at")

        return queryset
于 2021-11-02T19:14:47.823 回答