Last active
July 6, 2016 06:51
-
-
Save kyle-eshares/726d001cd33ced51fb86e1612f1ff72d to your computer and use it in GitHub Desktop.
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
from django.db import models | |
from django.db.models import signals | |
from django.contrib.admin.utils import NestedObjects | |
class AppQuerySet(models.QuerySet): | |
def delete(self, **kwargs): | |
return self.update(is_void=True) | |
class AppManager(models.Manager): | |
queryset_class = AppQuerySet | |
use_for_related_fields = True | |
def get_queryset(self, exclude_void=True): | |
q = self.queryset_class(self.model) | |
if hasattr(self, 'core_filters'): | |
q = q.filter( | |
**self.core_filters | |
) | |
if exclude_void: | |
q = q.exclude(is_void=True) | |
return q | |
def all_objects_including_void(self): | |
return self.get_queryset(exclude_void=False) | |
class AppModel(models.Model): | |
is_void = models.BooleanField(default=False) | |
objects = AppManager() | |
class Meta: | |
abstract = True | |
def delete(self, **kwargs): | |
self.is_void = True | |
self.save() | |
signals.post_delete.send( | |
sender=self.__class__, instance=self | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment