Here is what I have.
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))
And I'm getting
# >>> result.first().items
[4, 6, 10]
But I need the sum of all items(4+6+10)->20 for each A row.
Like this
# >>> result.first().items
20
Is it possible?