How to handle 50k or above array of objects in Django Rest Framework?

Viewed 127

I'm facing a problem. Our requirement is we cannot use pagination so we need all records at a time in Response.

my function like this:

from rest_framework.response import Response

class TestingViewsAPI(APIView):
    
    def get(self, request):
       data_list = [{}....................] #50k list of dict

       return Response({
                "Data": {
                       "data": data_list,
                }
        })
        

When doing this then my system hang how i can handle this? I need expert guidelines I don't use pagination

1 Answers

You are looking for StreamingHttpResponse:

The StreamingHttpResponse class is used to stream a response from Django to the browser. You might want to do this if generating the response takes too long or uses too much memory. For instance, it’s useful for generating large CSV files.

You should to avoid putting all data in memory. The way is to stream all workflow, from generating (or read) json to consume it on your client app.

Related