1

我试图摆脱自制解决方案以支持更标准的解决方案。我之前的模式:

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)

我想用 django-model-util's 替换它,SoftDeletableModel但我没有看到任何all_with_deleted类似的功能SoftDeletableManagerMixin- 它只会覆盖get_queryset,仅此而已。我的架构是分散的,当我通知其他节点有关软删除的信息时,我需要访问这些节点。

4

1 回答 1

1

所以我会这样解决这个问题:

from model_utils.models import SoftDeletableModel

class MyModel(SoftDeletableModel):
    all_objects = models.Manager()  # To access soft deleted objects as well

    name = models.CharField()
    ...

这样我可以通过说MyModel.all_objects.all()而不是我的旧模式来访问所有对象MyModel.objects.all_with_deleted().all()MyModel.objects.all()在这两种情况下都只会提供非软删除的。

于 2018-03-09T00:38:30.610 回答