Issue
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....
Solution
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
Answered By - token
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.