0

I have a class-based view for text messages inheriting from the django generic view ListAPIView. As you can probably tell from the inheriting class name, the view is being used as an API with pagination (from the Django Rest framework).

I would like to turn off pagination for some specific queries, however even when I try turning off pagination for all queries via this stack overflow question (Turn off automatic pagination of Django Rest Framework ModelViewSet), I get the following error:

RuntimeError: Do not evaluate the `.queryset` attribute directly, as the result will be cached and reused between requests. Use `.all()` or call `.get_queryset()` instead.

I am overwriting the get_queryset() method in the view without issue, however by just setting the paginator_class variable to None I am receiving this error. Any help would be appreciated. Here's some code:

view.py:

class TextMessageView(generics.ListAPIView):
    queryset = TextMessage.objects.all()
    serializer_class = TextMessageSerializer
    pagination_class = None


    def get_queryset(self):
        """
        If a phone number is included in the url query, return only the text messages sent by that number in the
        queryset, otherwise return all text messages.
        :return: A Django queryset with a variable number of text messages.
        """
        from_phone_num = self.request.query_params.get('from_phone_number', None)
        distinct_nums = self.request.query_params.get('distinct_numbers', None)
        all_msgs = self.request.query_params.get('all_messages', None)
        if from_phone_num:
            return TextMessage.objects.filter(from_phone_number=from_phone_num)
        elif distinct_nums == 'True':
            return TextMessage.objects.order_by('from_phone_number', '-date_of_message').distinct('from_phone_number')
        elif all_msgs == 'True':
            return self.queryset
        else:
            raise Http404
Django Rest Frameworks: ListAPIView.py

class ListAPIView(mixins.ListModelMixin,
                  GenericAPIView):
    """
    Concrete view for listing a queryset.
    """
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

Django Rest Frameworks: mixins.py

class ListModelMixin:
    """
    List a queryset.
    """
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

you'll have to look up GenericAPIView.py because it's too big

1 Answers1

2

Looking at the error message, in your get_queryset method, can you try changing

elif all_msgs == 'True':
            return self.queryset

to

elif all_msgs == 'True':
            return self.queryset.all()

?

storm143
  • 84
  • 8