Created
April 16, 2016 23:16
-
-
Save victor-o-silva/11d5efc05e0a557e6da73d6e54912115 to your computer and use it in GitHub Desktop.
An easily prettifiable JsonResponse for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import JsonResponse as DjangoJsonResponse | |
class JsonResponse(DjangoJsonResponse): | |
"""An easily prettifiable JsonResponse. | |
To prettify the response's content, instantiate this class passing | |
``pretty=True``. The content will be prettified with a level 4 indent. | |
""" | |
def __init__(self, *args, **kwargs): | |
if kwargs.pop('pretty', None) is True: | |
json_dumps_params = kwargs.get('json_dumps_params', {}) | |
json_dumps_params['indent'] = 4 | |
kwargs['json_dumps_params'] = json_dumps_params | |
super(JsonResponse, self).__init__(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test case (tested with Python 3.5.1 and Django 1.9.5)