from django.utils import six from rest_framework.response import Response from rest_framework.serializers import Serializer from django.http import HttpResponse
classJsonResponse(Response): """ 重写 from django.http import JsonResponse An HttpResponse that allows its data to be rendered into arbitrary media types. """
def__init__(self, data=None, code=200, msg="success", status=None, template_name=None, headers=None, exception=False, content_type=None, **kwargs): """ Alters the init arguments slightly. For example, drop 'template_name', and instead use 'data'. Setting 'renderer' and 'media_type' will typically be deferred, For example being set automatically by the `APIView`. """ super(Response, self).__init__(None, status=status) ifisinstance(data, Serializer): msg = ( 'You passed a Serializer instance as data, but ' 'probably meant to pass serialized `.data` or `.error`. representation.' ) raise AssertionError(msg) self.data = {"code": code, "message": msg, "data": data} self.data.update(kwargs) self.template_name = template_name self.exception = exception self.content_type = content_type if headers: for name, value in six.iteritems(headers): self[name] = value