Skip to content

Instantly share code, notes, and snippets.

@jadhavmanoj
Last active March 25, 2022 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jadhavmanoj/1d3b9e51942c9e1e9a88d4e0a1a20614 to your computer and use it in GitHub Desktop.
Save jadhavmanoj/1d3b9e51942c9e1e9a88d4e0a1a20614 to your computer and use it in GitHub Desktop.
Django Rest Framework (DRF) Soft delete Mixin.
# 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)
@ierika
Copy link

ierika commented Jun 20, 2021

Much better to do it in perform_destroy, I think.

def perform_destroy(self, instance):
    instance.is_active = False
    instance.save()

@Vrajesh-Vaghasiya
Copy link

Vrajesh-Vaghasiya commented Mar 25, 2022

@ierika I agree with you. This is better way to perform delete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment