[Fixed]-How to get pretty output from rest_framework serializer

10👍

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.

12👍

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',
    )
}

10👍

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)
👤brnt

2👍

use below line

import json
return StreamingHttpResponse(json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')),   mimetype='application/json')

0👍

Not sure if this serves your use case best, but I found out i get a more readble result when i call pprint on the dict() of a ReturnDict object:

ipdb> type(self.response.body)
<class 'rest_framework.utils.serializer_helpers.ReturnDict'>
ipdb> dict(self.response.body)
{'status': 'Open', ..., 'voucher_discounts': []}  # all on one line
ipdb> pprint(dict(self.response.body))
{'currency': u'EUR',
 'id': 1,
 'lines': [OrderedDict([('url', u'http://testserver/api/baskets/1/lines/1/'), ...]), ...],
 ...
 'voucher_discounts': []}

Still it won’t format into the list of dicts in ‘lines’ though.

👤khink

0👍

You can do this in your view by adding headers={'indent': ' '} in response. Try this:

class JSONListView(ReadOnlyModelViewSet):
    queryset = YourModel.objects.all()
    serializer_class = YourSerializer

    def get_renderers(self):
        return [JSONRenderer()]

    def get_renderer_context(self):
        serializer = self.get_serializer(self.get_queryset(), many=True)
        response = Response(data=serializer.data, headers={'indent': '    '})
        return response

Leave a comment