How to get pretty output from rest_framework serializer
When you are using rest_framework you shouldn't use json.dumps
yourself, since the renderers do this work for you. The data you see is the python dictionary, which is the output of the serializer. It does not get rendered by DRF, because you are are returning a Django StreamingHttpResponse
. This data needs to be rendered to get your JSON.
Is there a reason for you bypassing the rest_framework rendering?
Otherwise, this is your Handler:
return Response(InstallSerializer(Install.objects.all(), many=True).data)
Where Response
is rest_framework.response.Response
.
If your client needs pretty json: The rest_framework JSONRenderer
supports the indent
argument for the Accept
header (see the docs).
So when your client sends:
Accept: application/json; indent=4
Your JSON will be indented.
use below line
import json
return StreamingHttpResponse(json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
In addition to Denis Cornehl's answer, you can also force pretty-printed output without the client specifying such in its Accept:
header. Instead, you specify it in the renderer_context
parameter when you call render()
like this:
content = JSONRenderer().render(data, renderer_context={'indent':4})
By tweaking the example from the Django Rest Framework's tutorial, you can pretty-print all JSON serialized objects:
class JSONResponse(HttpResponse):
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data, renderer_context={'indent':4})
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
I did this with:
class PrettyJsonRenderer(JSONRenderer):
def get_indent(self, accepted_media_type, renderer_context):
return 2
And then specify PrettyJsonRenderer
in your site's settings.py
file:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'myapp.util.PrettyJsonRenderer',
)
}