Using django message framework with rest_framework

rjv picture rjv · Dec 13, 2014 · Viewed 10.7k times · Source

How can I have the django message framework work with the rest_framework?

Here is my view

@api_view(['GET', 'POST'])
def myview(request):
    if request.method == 'GET':
        #return a Response object
    else:
        #process post data
        messages.success(request, 'Success')
        return Response(response)

I encounter the following error

add_message() argument must be an HttpRequest object, not 'Request'

which is because the rest_framework does not use the normal HttpRequest object, used in django by default.

How can I use messaging framework with rest framework?

Answer

sax picture sax · Dec 13, 2014

DRF views do not use HttpRequest but use rest_framework.request.Request, (read here) you can access to the object that you need using

 messages.success(request._request, 'Success')

anyway this code have sense only if you are using BrowsableAPIRenderer