1

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.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121

1 Answers1

1

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.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
  • 2
    In latest django-model-utils the `all_objects` manager [already comes from upstream](https://github.com/jazzband/django-model-utils/blob/master/model_utils/models.py#L126). You don't even need to set it up. – Pablo M Nov 27 '19 at 19:25