pythondjangodjango-rest-framework

DRF self.paginate_queryset(queryset) doesn't respect order from self.get_queryset()


i have a ListAPIView with custom get_queryset() where it ordered by a custom annotate() field called 'mentions'

i have customized my list function in ListAPIView, but when calling self.paginate_queryset() with the queryset i'm getting different result(not getting the first data from queryset

class KeywordList(ListAPIView):
    """return list of keywords
    """
    pagination_class = CursorPagination
    serializer_class = ListKeywordSerializer
    total_reviews_count = Review.objects.all().count()
    ordering = ["-mentions"]

    def get_queryset(self):
        queryset = Keyword.objects.all()
        queryset = queryset.annotate(...).order_by('-mentions')

  
    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        print('queryset first', queryset[0])
        page = self.paginate_queryset(queryset)
        print('page first', page[0])

here is the print log:

queryset first good
page first design program
    

as you can see, i'm getting different result(first index) after running the queryset through self.paginate_queryset(queryset)

how can i fix this?


Solution

  • nvm, i went into the code base of CursorPagination, it forced ordering by -created

    class CursorPagination(BasePagination):
        """
        The cursor pagination implementation is necessarily complex.
        For an overview of the position/offset style we use, see this post:
        https://cra.mr/2011/03/08/building-cursors-for-the-disqus-api
        """
        cursor_query_param = 'cursor'
        cursor_query_description = _('The pagination cursor value.')
        page_size = api_settings.PAGE_SIZE
        invalid_cursor_message = _('Invalid cursor')
        ordering = '-created' <- forced ordering
    

    so i made my own custom pagination class and solved this:

    class KeywordCursorPagination(CursorPagination):
        page_size = 10
        ordering = '-mentions'