Using django message framework with rest_framework

12.4k Views Asked by At

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?

1

There are 1 best solutions below

4
On BEST ANSWER

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