mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
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:
parent
e33fed70d6
commit
aa9e6c2de5
|
@ -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()
|
||||||
|
@ -75,6 +76,12 @@ class UpdateModelMixin(object):
|
||||||
def partial_update(self, request, *args, **kwargs):
|
def partial_update(self, request, *args, **kwargs):
|
||||||
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):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user