结合django经理

时间:2012-12-28 21:10:37

标签: python django django-models django-managers

我有两个django经理

投票经理

class VoteManager(model.Manager):

    def all_with_vote_info(self):
        qs = super(VoteManager, self).get_query_set()
        qs = qs.annotate(score=Sum('votes__score', distinct=True))
        return qs

    ....

软删除管理器

class SoftDeleteManager(models.Manager):

    def all_active(self):
        qs = super(SoftDeleteManager, self).get_query_set()
        qs = qs.filter(time_deleted=None)
        return qs

    ....

如何从VoteManager.all_with_vote_infoSoftDeleteManager.all_active和任意数量的经理方法中链接查询集结果?

2 个答案:

答案 0 :(得分:2)

找到解决方案:PassThroughManager

https://django-model-utils.readthedocs.org/en/latest/managers.html#passthroughmanager

<强>更新

PassThroughManager已被弃用,请改用Django的内置QuerySet.as_manager()和/或Manager.from_queryset()工具。

答案 1 :(得分:0)

使用此方法PolymorphicQuerySet方法都可用,然后我们可以从TimeseriesQueryset注册其他方法

class UserPartQuerySet(PolymorphicQuerySet, TimeSeriesQuerySet):


    def prefetch_latest(self, *related_names):
        return TimeSeriesQuerySet.prefetch_latest(self, *related_names)

    def filter_outdated(self, *related_names):
        return TimeSeriesQuerySet.filter_outdated(self, *related_names)

    def update_timeseries(self, related_name, collector, force=False):
        return TimeSeriesQuerySet.update_timeseries(
                self,
                related_name,
                collector,
                force=False
        )