Вот что у меня есть.
class Array(Subquery):
template = 'ARRAY(%(subquery)s)'
class A(models.Model):
...
class B(models.Model):
a = models.ForeignKey(A)
class C(models.Model):
b = models.ForeignKey(B)
b_sub = B.objects.filter(a=OuterRef('pk').annotate(items=Count('c').values('items')
result = a.objects.annotate(items=Array(b_sub))
И я получаю
# >>> result.first().items
[4, 6, 10]
Но мне нужна сумма всех items
(4 + 6 + 10) -> 20 для каждой A
строки.
Как это
# >>> result.first().items
20
Возможно ли это?