Micheal J. Roberts
Micheal J. Roberts

Reputation: 4180

Django REST Framework - paginate_queryset() takes 2 positional arguments but 3 were given

Really not sure what I have done wrong here, minus a few details, it's very similar to both examples on the REST framework website and almost a carbon copy of another Viewset I have.

class ArtistViewSet(
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    viewsets.GenericViewSet,
    ArtistSetPagination
):
    queryset = Profile.objects.all()
    permission_classes = [permissions.AllowAny,]
    pagination_class = ArtistSetPagination
    serializer_class = ProfileSerializer

    def get_queryset(self, *args, **kwargs):
        return Profile.objects.all().prefetch_related(
            'user'
        )

    # Artwork List & List Filtering API View:
    def list(self, request, *args, **kwargs):
        parameters = {key: request.GET.get(key) for key in dict(request.GET).keys()}

        queryset = self.get_queryset().annotate(
            first_name_len=Length('user__first_name'),
            last_name_len=Length('user__last_name')
        ).filter(
            first_name_len__gt=0,
            last_name_len__gt=0,
        ).filter(
            **parameters
        ).order_by(
            '-created'
        )

        page = self.paginate_queryset(queryset, self.request)

        if page is not None:
            serializer = ProfileSerializer(page, context={'request': request}, many=True)
            data = paginated.data
            paginated = self.get_paginated_response(data)
        else:
            serializer = ProfileSerializer(queryset, context={'request': request}, many=True)
            data = serializer.data

        response = standardized_json_response(
            message='Artist Objects Has Been Successfully Listed',
            timestamp=datetime.datetime.now(),
            data=data
        )

        return Response(data=response, status=status.HTTP_200_OK)

All I can see that is different is the use of the annotation for Length()...

If anyone has any pointers I would gladly accept.

N.B. The standardised_json_response() is just a wrapper which takes a few variables and returns a dict() object, just injecting some extra meta such as the timestamp of the request and the success status (True/False)

Upvotes: 0

Views: 3781

Answers (1)

Sadraoui Taha
Sadraoui Taha

Reputation: 304

As its name indicates, paginate_queryset paginates a given queryset. This is its definition:

def paginate_queryset(self, queryset):
        """
        Return a single page of results, or `None` if pagination is disabled.
        """

While here you are calling it with an additional argument self.request: page = self.paginate_queryset(queryset, self.request), instead of page = self.paginate_queryset(queryset)

Upvotes: 1

Related Questions