Skip to content

Instantly share code, notes, and snippets.

@a4amaan
Forked from jadhavmanoj/DRF Soft delete mixin
Created April 8, 2020 18:13
Show Gist options
  • Save a4amaan/4c90cc014692104853df4264348f729d to your computer and use it in GitHub Desktop.
Save a4amaan/4c90cc014692104853df4264348f729d 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment