Reputation: 2687
I'm trying to serialize more than one queryset, but I noticed that only one of them becomes serialized. This is the approach I'm currently trying.
class GetDetails(APIView):
def get(self, request):
todays_date = time.strftime('%Y-%m-%d')
#Get results according to specified criteria
queryset = People.objects.filter(date = todays_date, assigned_to = 1)
#Check if any querysets are available
if queryset:
#Iterate through each queryset, serialize and return a response
for person in queryset:
serializer=ASerializer(person)
return Response(serializer.data)
else:
return Response({'TODO':'TODO'})
Upvotes: 1
Views: 97
Reputation: 3058
First of all the way you are performing queryset is not right, you are iterating over queryset with person variable and you are not using person variable at all.
And for the question, use many=True
attribute of serializer. It will create a list of serialized items for you.
like this:
class GetDetails(APIView):
def get(self, request):
todays_date = time.strftime('%Y-%m-%d')
queryset = People.objects.filter(date = todays_date, assigned_to = 1)
return Response(ASerializer(many=True).to_representation(queryset))
Upvotes: 2
Reputation: 52018
Maybe your code should be like this:
def get(self, request):
todays_date = time.strftime('%Y-%m-%d')
queryset = People.objects.filter(date = todays_date, assigned_to = 1)
if queryset.exists(): # Lazy check if entry Exists.
serializer=ASerializer(queryset, many=True)
return Response(serializer.data)
else:
return Response({'TODO':'TODO'})
Upvotes: 1