'Django ORM query works on shell but time outs in Api View

I have large list of ids and wanted to filter objects using those ids:

list_of_ids = [1, 2, 3, 4, ...]

MyModel.objects.filter(id__in=list_of_ids)[:20]

That query works just fine from shell, but when I try to do the same thing in API View it time outs:

from rest_framework.response import Response
from rest_framework.views import APIView


class MyModelAPIView(APIView):
    def get(self, request):
        ids = [1, 2, 3, 4, 5, ...]
        print(MyModel.objects.filter(id__in=ids)[:20])
        return Response({})
    

After I got the response with a 504 status code, I see in terminal logs what the print function did:

django_1       | <QuerySet [<MyModel: 231>, <MyModel: 268>, <MyModel: 269>, <MyModel: 271>, <MyModel: 272>, <MyModel: 274>, <MyModel: 275>, <MyModel: 277>, <MyModel: 280>, <MyModel: 281>, <MyModel: 282>, <MyModel: 284>, <MyModel: 285>, <MyModel: 288>, <MyModel: 289>, <MyModel: 290>, <MyModel: 291>, <MyModel: 292>, <MyModel: 294>, <MyModel: 298>]>

But I can't find out why do request time outs? I could not find similar problems there

(I understand that problem might be in my large list of ids, but shell executes that query without any problem)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source