Skip to content

Instantly share code, notes, and snippets.

@kyle-eshares
Created June 27, 2016 23:03
Show Gist options
  • Save kyle-eshares/6e2d2d892ec477bb07fd66f9d9998e26 to your computer and use it in GitHub Desktop.
Save kyle-eshares/6e2d2d892ec477bb07fd66f9d9998e26 to your computer and use it in GitHub Desktop.
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 AdvancedManager(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 = AdvancedManager()
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