Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentNotRenderedError: The response content must be rendered before it can be iterated over. Django REST

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....

like image 683
satyajit Avatar asked Nov 08 '25 02:11

satyajit


1 Answers

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
like image 112
token Avatar answered Nov 10 '25 15:11

token



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!