aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Recio2013-12-03 00:07:41 +0000
committerPablo Recio2013-12-03 00:07:41 +0000
commit699ec7236b326c97a98c6058280b822c701393fe (patch)
tree441d1f800e4d2cd02d2858bb8e177b9e39fb9805
parent01040b077c16f69101249282b62506f08ebff651 (diff)
downloaddjango-rest-framework-699ec7236b326c97a98c6058280b822c701393fe.tar.bz2
Adds pre_delete and post_delete hooks on
-rwxr-xr-xdocs/api-guide/generic-views.md4
-rw-r--r--rest_framework/generics.py12
-rw-r--r--rest_framework/mixins.py2
3 files changed, 17 insertions, 1 deletions
diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md
index b9242724..83c3e45f 100755
--- a/docs/api-guide/generic-views.md
+++ b/docs/api-guide/generic-views.md
@@ -163,12 +163,14 @@ For example:
return 20
return 100
-**Save hooks**:
+**Save / deletion hooks**:
The following methods are provided as placeholder interfaces. They contain empty implementations and are not called directly by `GenericAPIView`, but they are overridden and used by some of the mixin classes.
* `pre_save(self, obj)` - A hook that is called before saving an object.
* `post_save(self, obj, created=False)` - A hook that is called after saving an object.
+* `pre_delete(self, obj)` - A hook that is called before deleting an object.
+* `post_delete(self, obj)` - A hook that is called after deleting an object.
The `pre_save` method in particular is a useful hook for setting attributes that are implicit in the request, but are not part of the request data. For instance, you might set an attribute on the object based on the request user, or based on a URL keyword argument.
diff --git a/rest_framework/generics.py b/rest_framework/generics.py
index 7cb80a84..fd411ad3 100644
--- a/rest_framework/generics.py
+++ b/rest_framework/generics.py
@@ -344,6 +344,18 @@ class GenericAPIView(views.APIView):
"""
pass
+ def pre_delete(self, obj):
+ """
+ Placeholder method for calling before deleting an object.
+ """
+ pass
+
+ def post_delete(self, obj):
+ """
+ Placeholder method for calling after saving an object.
+ """
+ pass
+
def metadata(self, request):
"""
Return a dictionary of metadata about the view.
diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py
index 79f79c30..43950c4b 100644
--- a/rest_framework/mixins.py
+++ b/rest_framework/mixins.py
@@ -192,5 +192,7 @@ class DestroyModelMixin(object):
"""
def destroy(self, request, *args, **kwargs):
obj = self.get_object()
+ self.pre_delete(obj)
obj.delete()
+ self.post_delete(obj)
return Response(status=status.HTTP_204_NO_CONTENT)