I have Django 1.10 project and the following user-defined middleware
class RequestLogMiddleWare(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
response.data['detail'] = 'I have been edited'
return response
and a REST-endpoint view:
def r_mobile_call_log(request):
return Response({'success': True,
'detail': 'Before having been edited'},
status=status.HTTP_200_OK)
So I would expect the final response on client-side to be:
{'success': 'True', 'detail': 'I have been edited'}
However, what I see is:
{'success': 'True', 'detail': 'Before having been edited'}
I put a breakpoint in the middleware's call method to make sure that the function really is executed, and it's ok. response.data["details"]
just won't change it's value. Anyone knows what's the reason for this ?
Response
is already rendered in the middleware stage so you can't just changeresponse.data
, you need to rerender it or change rendered content directly.The second approach is just change content directly, but in that case built in rest framework browser API will not work because template will not render properly.
source code for render method
I have a feeling that I found cleaner solution. Here's how I rewrote the code: