,总价值比方说,我有以下的模型结构:Django的注释模型基于查询
Parent():
Child():
parent = ForeignKey(Parent)
GrandChild():
child = ForeignKey(Child)
state = BooleanField()
num = FloatField()
我从父ViewSet
努力,恢复如下:
- 的儿童人数。
- '状态'为True时'num'字段的和。
我可以做到以下几点:
queryset = Parent.objects\
.annotate(child_count=Count('child'))\
.annotate(sum_total=Sum('child__grandchild__num'))
这给了我(1),但不是(2)它给了我为所有孙子的总和。我应该如何筛选孙辈,同时确保所有Parent
对象仍在QuerySet中?
这将工作,但我需要确保我有所有的父对象。对不起,我会编辑最初的问题。 – Eric
@Eric您将从此获得父对象queryset。 –
如果特定的父母没有GrandChild,该怎么办? – Eric