[Django]-How to combine two similar views into a single response?

6👍

Since serializer.data will be a list of dictionaries in your case, I’d try concatenating the results of both serializers for the response:

class GetFoosAndResponses(generics.ListAPIView):
    # init stuff here

    def list(self, request):
        foo_queryset = Foo.objects.filter(owner=request.user)
        foo_serializer = FooSerializer(foo_queryset, many=True)

        foo_response_queryset = FooResponse.objects.filter(owner=request.user)
        foo_response_serializer = FooResponseSerializer(foo_response_queryset, many=True)

        return Response(foo_serializer.data + foo_response_serializer.data, status=status.HTTP_200_OK)

Does this make sense? 🙂

Leave a comment