Skip to content

Instantly share code, notes, and snippets.

@jadhavmanoj
Last active March 25, 2022 06:35
Show Gist options
  • 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)
@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