status_code in Renderer class

33 views
Skip to first unread message

Stefano Tranquillini

unread,
Jan 29, 2016, 8:32:33 AM1/29/16
to Django REST framework
Hello,
i'm trying to use DRF to see if i can setup a project i've in mind.
I want to have always  as a response an Json object that has "message", "data", "result", "result_code".
I did so by creating a Renderer, that works. 

The problem is: how can i get access to the response.status_code in the renderer?

If there's an exception i do this via an exception handler
    response.data['status_code'] = response.status_code

but what when everything works? how can i always set that parameter?

Xavier Ordoquy

unread,
Jan 29, 2016, 9:00:39 AM1/29/16
to django-res...@googlegroups.com
Hi Stefano,

You’ll usually override Renderer.render so you should have access to render’s renderer_context which contains the response:

Regards,
Xavier,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefano Tranquillini

unread,
Feb 1, 2016, 4:50:11 AM2/1/16
to Django REST framework
Any link to docs that can guide me in implementing something?

Xavier Ordoquy

unread,
Feb 1, 2016, 4:53:57 AM2/1/16
to django-res...@googlegroups.com

I don’t think so, however, the sources are available and should show you how to do it.

Regards,
Xavier,
Linovia.

Stefano Tranquillini

unread,
Feb 1, 2016, 9:34:40 AM2/1/16
to django-res...@googlegroups.com
I didn't get what you were suggesting at first.
the trick is here: http://www.django-rest-framework.org/api-guide/renderers/#custom-renderers and the renderer_context can be access to get the access_code is
renderer_context['response'].status_code

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/j0kgZ0S6HFo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Stefano
Reply all
Reply to author
Forward
0 new messages