Skip to content

Instantly share code, notes, and snippets.

@i
Last active July 19, 2018 18:50
Show Gist options
  • Save i/a3e6582af834415aa1392da0bcd71cc5 to your computer and use it in GitHub Desktop.
Save i/a3e6582af834415aa1392da0bcd71cc5 to your computer and use it in GitHub Desktop.
class SoftDeleteQuerySet(models.QuerySet):
@transaction.atomic
def delete(self, snapshot_id=None):
[x.delete(snapshot_id=snapshot_id) for x in self]
class SoftDeleteQueryManager(models.Manager):
def __init__(self, *args, **kwargs):
self.with_deleted = kwargs.pop('with_deleted', False)
super(SoftDeleteManager, self).__init__(*args, **kwargs)
# same fields and methods before except for
def get_queryset(self):
qs = SoftDeleteQuerySet(self.model)
if self.with_deleted:
return qs
return qs.filter(is_deleted=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment