rest_framework_dso.response module

Streaming rendering support on top of Django-Rest-Framework.

Django has the internal support for streaming responses, but REST Framework does not. This module bridges that feature into the response format that Django-Rest-Framework’s rendering classes need.

This generic StreamingResponse class allows responses to be submitted as streaming. A streaming response can submit large amounts of data without consuming too much memory. Each time a bit of the response is generated, it’s immediately written to the client.

The rendered data also needs to be generated on consumption to have the full benefits of streaming. The DSOListSerializer achieves this by returning the results as a Python generator instead of a pre-rendered list.

class rest_framework_dso.response.StreamingResponse(data=None, status=None, template_name=None, headers=None, exception=False, content_type=None)

Bases: StreamingHttpResponse

A reimplementation of the DRF ‘Response’ class that provides actual streaming content.

While it’s possible to submit a generator to the DRF Response class, it will concatenate the results in-memory when reading the HttpResponse.content property.

__init__(data=None, status=None, template_name=None, headers=None, exception=False, content_type=None)

Init parameters are similar to the DRF Response class.

classmethod from_response(response: Response)

Convert a regular DRF Response into this streaming response. This is to be called from the APIView.finalize_response() function.

render_exception(exception)

Render an exception message in case the stream rendering fails. Otherwise, the response just closes and the client may assume it received the whole file. The actual exception is still raised and logged server-side.

property status_text

Copied from DRF Response class