我正在寻找一种获取价值总和的方法
class ModelX(models.Model):
name = models.CharField(...)
class ModelA(models.Model):
value = models.IntegerField()
class ModelB(models.Model):
modela = models.ForeignKey(ModelA)
modelxs = models.ManyToManyField(ModelX, through='ModelC')
class ModelC(models.Model):
modelb = models.ForeignKey(ModelB)
modelx = models.ForeignKey(ModelX)
modelxx = models.ForeignKey(ModelX, related_name='modelcs')
Modelx.objects.values('name').annotate(
total=Sum('modelcs__modelb__modela__value')
).values('total')
和
sum_qs = (
ModelA
.objects
.filter(modelb_set__modelc_set__modelxx=OuterRef('id'))
.values('modelb_set__modelc_set__modelxx_id')
.order_by()
.distinct()
.values('value')
)
qs = ModelX.objects.annotate(
total=Subquery(sum_qs.values('value')))
下面是示例模型,我希望上面看到预期结果中所述的10个:编辑
foo = ModelX.objects.create(name='Foo')
bar = ModelX.objects.create(name='Bar')
baz = ModelX.objects.create(name='Baz')
modela1 = ModelA.objects.create(value=10)
modela2 = ModelA.objects.create(value=10)
modelb1 = ModelB.objects.create(modela=modela1)
modelb2 = ModelB.objects.create(modela=modela2)
modelc1 = ModelC.objects.create(modelb=modelb1, modelx=foo, modelxx=bar)
modelc2 = ModelC.objects.create(modelb=modelb1, modelx=bar, modelxx=bar) # This would be a cause a duplicate as a relation to ModelX through modelxx
modelc3 = ModelC.objects.create(modelb=modelb1, modelx=baz, modelxx=baz)
modelc4 = ModelC.objects.create(modelb=modelb2, modelx=bar, modelxx=bar)
编辑 {'Bar':20,'Baz':10}
这也可以作为要序列化的ModelX的QuerySet