如何通过混合继承多个查询集过滤器

How to inherit multiple queryset filters via mixins

我在模型管理器中有一个QuerySetMixin

models.py:

class MyModel(models.Model):
    objects = SoftDeletableManager()

managers.py:

class SoftDeletableManager(SoftDeletableManagerMixin, models.Manager):
    pass

class SoftDeletableQuerySet(QuerySet):
    pass

class SoftDeletableManagerMixin:
    _queryset_class = SoftDeletableQuerySet

    def get_queryset(self):
         return self._queryset_class(
             model=self.model, 
             using=self._db,
             **kwargs).filter(is_removed=False)

我想定义第二个 QuerySetMixin 来继承 SoftDeletableManagerMixin 的结果并过滤它们。我该怎么做?

例如

class MyManagerMixin:

    def get_queryset(self):
        return self.[inherit other querysets].filter(mynewfilter=True)

如果继承它,您可能想使用 super() proxy object:

class SoftDeletableManagerMixin:
    _queryset_class = SoftDeletableQuerySet

    def get_queryset(self):
         return <b>super().get_queryset()</b>.filter(is_removed=False)

第二个 mixin 也一样:

class MyManagerMixin:

    def get_queryset(self):
        return <b>super().get_queryset()</b>.filter(mynewfilter=True)

然后您可以通过以下方式创建经理:

class MyNewManager(<b>MyManagerMixin, SoftDeletableManagerMixin</b>, Manager):
    pass