4

I am getting an error : The response content must be rendered before it can be iterated over.

What's wrong with below line of code..?? if anybody could figure out where i have done mistakes then would be much appreciated.
thank you so much in advance.

serializers.py


class GlobalShopSearchList(ListAPIView):
   serializer_class = GlobalSearchSerializer

   def get_queryset(self):
        try:
            query = self.request.GET.get('q', None)
            print('query', query)

            if query:
                snippets = ShopOwnerAddress.objects.filter(
                        Q(user_id__name__iexact=query)|
                        Q(user_id__mobile_number__iexact=query)|
                        Q(address_line1__iexact=query)
                    ).distinct()

                return Response({'message': 'data retrieved successfully!', 'data':snippets}, status=200)

            else:
                # some logic....

        except KeyError:
            # some logic....

satyajit
  • 666
  • 1
  • 10
  • 25

1 Answers1

4

get_queryset should return queryset, not Response.

Try it that way:

class GlobalShopSearchList(ListAPIView):
    serializer_class = GlobalSearchSerializer
    queryset = ShopOwnerAddress.objects.all()

    def get_queryset(self):
        queryset = super().get_queryset()

        query = self.request.GET.get('q')
        print('query', query)

        if query:
            queryset = queryset.filter(
                    Q(user_id__name__iexact=query)|
                    Q(user_id__mobile_number__iexact=query)|
                    Q(address_line1__iexact=query)
                ).distinct()

        return queryset
token
  • 903
  • 1
  • 9
  • 23