I'm try to get rid of home-brew solution to favor more standard ones. My previous pattern:
class MarkDeleteManager(models.Manager):
use_for_related_fields = True
def get_queryset(self):
if "instance" in self._hints:
return super(MarkDeleteManager, self).get_queryset()
return super(MarkDeleteManager, self).get_queryset().filter(deleted=False)
def all_with_deleted(self):
return super(MarkDeleteManager, self).get_queryset()
def deleted_set(self):
return super(MarkDeleteManager, self).get_queryset().filter(deleted=True)
def using(self, *args, **kwargs):
''' if a specific record was requested, return it even if it's deleted '''
return self.all_with_deleted().using(*args, **kwargs)
I'd like to replace this with django-model-util's SoftDeletableModel
but I don't see any all_with_deleted
like functionality in the SoftDeletableManagerMixin
- it only overrides get_queryset
and that's it. My architecture is decentralized, and when I notify other nodes about soft deletions I need to access those.
So I'll solve this that way:
from model_utils.models import SoftDeletableModel
class MyModel(SoftDeletableModel):
all_objects = models.Manager() # To access soft deleted objects as well
name = models.CharField()
...
This way I can access all the objects by saying MyModel.all_objects.all()
instead of my old pattern's MyModel.objects.all_with_deleted().all()
. MyModel.objects.all()
will provide only the non soft deleted ones in both cases.