suraj sharma
suraj sharma

Reputation: 413

field_name = ordering[0].lstrip('-') throws IndexError: tuple index out of range in DRF rest_framework\pagination.py

I am getting error IndexError: tuple index out of range while using CursorPagination of DRF. my code-

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

The errors comes when the entry in the database table for a particular user cross the value of page_size.

Ex- if some user have 5 payment order then there will be no error but when the same user cross 10 payment order then this error occurs.

Upvotes: 1

Views: 125

Answers (1)

Mohammadali Davarzani
Mohammadali Davarzani

Reputation: 29

I think you must order data in queryset, not in pagination.
for example:

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

Upvotes: 0

Related Questions