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',
)
}
- Django: Natural Sort QuerySet
- How to store django objects as session variables ( object is not JSON serializable)?
- Django Check and set permissions for a user group
- Migrate datetime w. timezone in PostgreSQL to UTC timezone to use Django 1.4
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)
- How to create or register User using django-tastypie API programmatically?
- Best Django 'CMS' component for integration into existing site
- Django import export – Unable to import model with BinaryField
- Creating a Gin Index with Trigram (gin_trgm_ops) in Django model
2👍
use below line
import json
return StreamingHttpResponse(json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
- Static files won't load when out of debug in Django
- Determine empty template variable in Django
- How to delete first N items from queryset in django
- Resize image on save
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.
- How to embed matplotlib graph in Django webpage?
- Inline-like solution for Django Admin where Admin contains ForeignKey to other model
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
- How can I automatically let syncdb add a column (no full migration needed)
- Django – How to send a success message using a UpdateView CBV