Created
October 28, 2015 20:21
-
-
Save njamaleddine/7bef4b656444de22ef04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- 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