我有两个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_info
,SoftDeleteManager.all_active
和任意数量的经理方法中链接查询集结果?
答案 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
)