How django-rest-framework customizes json data structures

< H1 > how django-rest-framework customizes json data structures < / H1 > The json data structure returned by the

framework is data (a list), but I need to add the business response code to this data. The response information structure is as follows:

{
    "data": data,
    "code": code,
    "msg": msg
}

I have considered modifying the json structure in the middleware, but because the framework"s response class has a render state, it can only be rendered once, so I give up the middleware method and want to find a new method:)


if I understand correctly, the code you defined is the http status code.
then please see the following actions, otherwise please bypass ~

Don't talk too much nonsense, just go to the code:

from reset_framework.views import APIview
from reset_framework.reponse import Response

class test(APIView):
    def get(self, request):
    -sharp do somethings
    return Response(data="you want response data", status=200)

where the Django HttpResponse encapsulated by Response in rest_framework also provides the status http status code.
BTW, also provides template_name, headers, exception, content_type, data, status .
I hope it will be helpful to you

Menu