2

I have looked around for the past few days and couldn't find a good way to do this. I found a couple other threads but nothing seemed to work the way I needed it to.

Here is the problem: I want to be able to serialize the result of multiple queries to return to the user so they don't have to connect to the API at multiple endpoints and so the server can do all of the heavy lifting when necessary.

I'm using APIView and a get request in views.py.

I have tried using chain from itertools but I can't seem to access the data from the serializer in any way. I have tried passing in a dictionary containing the results of the two queries and that didn't seem to work either.

There is probably a really simple way of solving this but I can't seem to find any documentation on it.

This is a simplified piece of code so someone could answer easily:

views.py

class ModelOutputList(APIView):
    def get(self, request):
        data = self.get_queryset()
        serializer = ModelOutputSerializer(
            data,
            many=True
        )

        return Response(serializer.data)

    def get_queryset(self):
        model1 = Model1.objects.all()
        model2 = Model2.objects.all()

        data = list(itertools.chain(model1, model2))

        return data

serializers.py

class ModelOutputSerializer(serializers.Serializer):
    model1 = Model1Serializer(many=True)
    model2 = Model2Serializer(many=True)

    class Meta:
        fields = ('model1', 'model2')

As always, any help would be greatly appreciated.

1 Answer 1

2

Try this:

views.py

class ModelOutputList(APIView):
    def get(self, request):
        obj = self.get_objects()
        serializer = ModelOutputSerializer(obj)

        return Response(serializer.data)

    def get_objects(self):
        model1 = Model1.objects.all()
        model2 = Model2.objects.all()

        obj = {'model1': model1, 'model2': model2}

        return obj

serializers.py

class ModelOutputSerializer(serializers.Serializer):
    model1 = Model1Serializer(many=True)
    model2 = Model2Serializer(many=True)

    class Meta:
        fields = ('model1', 'model2')
Sign up to request clarification or add additional context in comments.

7 Comments

Woops I didn't see the edit to views.py I will check right now. Thank you.
I get the following error: Got AttributeError when attempting to get a value for field model1 on serializer Model1Serializer. The serializer field might be named incorrectly and not match any attribute or key on the str instance. Original exception text was: 'str' object has no attribute 'model1'.
This will output the right json in the browser. The AttributeError must be raised from other code.
Did you use the same code I wrote? It should raise a TypeError: "get_objects() takes no arguments (1 given)" because I forget self: def get_objects(self)
Yeah, I had added self during testing and it is giving me the same issue. Have you used this code and it works fine for you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.