Commit 699ec723 by Pablo Recio

Adds pre_delete and post_delete hooks on

parent 01040b07
...@@ -163,12 +163,14 @@ For example: ...@@ -163,12 +163,14 @@ For example:
return 20 return 20
return 100 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. 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. * `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. * `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. 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.
......
...@@ -344,6 +344,18 @@ class GenericAPIView(views.APIView): ...@@ -344,6 +344,18 @@ class GenericAPIView(views.APIView):
""" """
pass 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): def metadata(self, request):
""" """
Return a dictionary of metadata about the view. Return a dictionary of metadata about the view.
......
...@@ -192,5 +192,7 @@ class DestroyModelMixin(object): ...@@ -192,5 +192,7 @@ class DestroyModelMixin(object):
""" """
def destroy(self, request, *args, **kwargs): def destroy(self, request, *args, **kwargs):
obj = self.get_object() obj = self.get_object()
self.pre_delete(obj)
obj.delete() obj.delete()
self.post_delete(obj)
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment