provide get_success_headers on UpdateModelMixin

This change provides a `get_success_headers` method on the UpdateModelMixin which can be used like the corresponding method on the CreateModelMixin. I thought this might be usefull because I was trying to add a header based on the result of the update operation, and this seems like a valid way.
This commit is contained in:
Konstantinos Koukopoulos 2015-05-18 18:42:21 +03:00
parent e33fed70d6
commit aa9e6c2de5

View File

@ -67,7 +67,8 @@ class UpdateModelMixin(object):
serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
self.perform_update(serializer) self.perform_update(serializer)
return Response(serializer.data) headers = self.get_success_headers(serializer.data)
return Response(serializer.data, headers=headers)
def perform_update(self, serializer): def perform_update(self, serializer):
serializer.save() serializer.save()
@ -76,6 +77,12 @@ class UpdateModelMixin(object):
kwargs['partial'] = True kwargs['partial'] = True
return self.update(request, *args, **kwargs) return self.update(request, *args, **kwargs)
def get_success_headers(self, data):
try:
return {'Content-Location': data[api_settings.URL_FIELD_NAME]}
except (TypeError, KeyError):
return {}
class DestroyModelMixin(object): class DestroyModelMixin(object):
""" """