Skip to content

Instantly share code, notes, and snippets.

@njamaleddine
Created October 28, 2015 20:21
Show Gist options
  • Select an option

  • Save njamaleddine/7bef4b656444de22ef04 to your computer and use it in GitHub Desktop.

Select an option

Save njamaleddine/7bef4b656444de22ef04 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from django.db import models
class SoftDeleteQuerySet(models.query.QuerySet):
"""
Override delete functionality so that it performs a soft delete on a
QuerySet unless delete_record() is explicitly called
The model being deleted needs to inherit from `models.ActiveModel`,
a model that (contains `is_active` boolean field)
"""
def delete(self):
self.update(is_active=False)
def delete_record(self):
self.delete()
def deleted(self):
self.filter(is_active=False)
class SoftDeleteManager(models.Manager):
"""
Soft Delete Manager that overrides a model's delete method to perform a
soft delete
"""
def get_queryset(self):
return SoftDeleteQuerySet(self.model)
def delete_record(self):
self.get_queryset().delete_record()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment