如何在Django orm查询中对具有重复行的值求和

时间:2019-04-19 13:27:17

标签: django django-orm

目标

我正在寻找一种获取价值总和的方法


模型

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')))

问题

  1. ModelC可以包含具有相同 modelxx 关系的多行。这将导致重复,并导致“值”的总和被重复

示例

下面是示例模型,我希望上面看到预期结果中所述的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


0 个答案:

没有答案