Last active
March 25, 2022 06:35
-
-
Save jadhavmanoj/1d3b9e51942c9e1e9a88d4e0a1a20614 to your computer and use it in GitHub Desktop.
Django Rest Framework (DRF) Soft delete Mixin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in DRF DestroyModelMixin delete row. Using Same DELETE method I can soft delete object. | |
# ex: URL - DELETE https://<hostnam>/api/v1/books/1/ | |
from rest_framework import mixins, permissions, viewsets | |
from rest_framework.response import Response | |
from rest_framework import status | |
class SlSoftDeleteMixin(mixins.DestroyModelMixin): | |
""" As we are deleting soft""" | |
def destroy(self, request, *args, **kwargs): | |
instance = self.queryset.get(id=kwargs.get('uuid')) | |
instance.is_active = False | |
instance.save() | |
return Response(status=status.HTTP_204_NO_CONTENT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much better to do it in
perform_destroy
, I think.