mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Adds pre_delete and post_delete hooks on
This commit is contained in:
parent
01040b077c
commit
699ec7236b
|
@ -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):
|
||||||
"""
|
"""
|
||||||
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):
|
||||||
"""
|
"""
|
||||||
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user