Created
February 25, 2015 21:47
-
-
Save dnozay/373571d8a276e6b2af1a to your computer and use it in GitHub Desktop.
django - QuerySet whose delete() marks items as deactivated rather than delete rows.
This file contains 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
# based on http://stackoverflow.com/questions/6459616/overriding-queryset-delete-in-django | |
class DeactivateQuerySet(models.query.QuerySet): | |
''' | |
QuerySet whose delete() does not delete items, but instead marks the | |
rows as not active, and updates the timestamps | |
''' | |
def delete(self): | |
self.deactivate() | |
def deactivate(self): | |
deleted = now() | |
self.update(active=False, modified=deleted, deleted=deleted) | |
def active(self): | |
return self.filter(active=True) | |
class DeactivateManager(models.Manager): | |
''' | |
Manager that returns a DeactivateQuerySet, | |
to prevent object deletion. | |
''' | |
def get_query_set(self): | |
return DeactivateQuerySet(self.model, using=self._db) | |
def active(self): | |
return self.get_query_set().active() | |
class DeactivateMixin(models.Model): | |
''' | |
abstract class for models whose rows should not be deleted but | |
items should be 'deactivated' instead. | |
note: needs to be the first abstract class for the default objects | |
manager to be replaced on the subclass. | |
''' | |
active = models.BooleanField(default=True, editable=False, db_index=True) | |
deleted = models.DateTimeField(default=None, editable=False, null=True) | |
objects = DeactivateManager() | |
class Meta: | |
abstract = True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment