I am creating an API that requires a custom structure for all JSON responses.
{
code: 200,
payload: [
{...},
{...},
{...}
]
}
Where payloadcontains all the items returned by the request.
I created a custom Rendererone that extends JSONRenderer, but to access the response code I need to access render_context.
class VendorRenderer(JSONRenderer):
def render(self, data, accepted_media_type=None, render_context=None):
response = render_context['response']
data = {
'code': response.status_code,
'payload': data
}
return super(VendorRenderer, self).render(data, accepted_media_type, render_context)
Is this a suitable place for this kind of wrapping, or should it happen somewhere else, such as a ViewSet, or by extending the Response object?
source
share