Skip to content

Instantly share code, notes, and snippets.

@dkdndes
Last active January 21, 2017 16:38
Show Gist options
  • Select an option

  • Save dkdndes/9616694 to your computer and use it in GitHub Desktop.

Select an option

Save dkdndes/9616694 to your computer and use it in GitHub Desktop.
Serialize to JSON when Django-rest-framework or Tastypie is to heavy
import json
from django.http import HttpResponse
class JSONResponse(HttpResponse):
"""
Return a JSON serialized HTTP resonse
"""
def __init__(self, request, data, status=200):
serialized = json.dumps(data)
super(JSONResponse, self).__init__(
content=serialized,
content_type='application/json'
status=status
)
class JSONViewMixin(object):
"""
Add this mixin to a Django CBV subclass to easily return JSON data.
"""
def json_response(self, data, status=200):
return JSONResponse(self.request, data, status=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment