Correct way to pass huge JSON API response (Django REST)

荒凉一梦 提交于 2021-01-29 17:04:06

问题


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:

  1. Create temp file and store all chunks there - then stream with StreamHttpResponse.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!