问题
I have a task to pass huge JSON response and would like to understand what is the best practice for that.
What I'm doing right now is using StreamHttpResponse
:
def generator_chunk():
...
yield df.to_json # returns data like [json]
@api_view(['GET'])
def return_data(request):
...
return StreamHttpResponse(generator_chunk) # streams data like [json][json], which is not proper json format
Few options I have right now on how to solve it:
- Create temp file and store all chunks there - then stream with
StreamHttpResponse
. - Use
PageNumberPagination
or smth similar - to have next/prev links.
These 2 approaches are new to me, so I want to understand which one is better. Especially interested in option 1. Or if neither, how it can be handled correctly.
P.S. What I mean by huge - if table results count is > # of rows that can be read from database at once.
来源:https://stackoverflow.com/questions/62711507/correct-way-to-pass-huge-json-api-response-django-rest