Djang中参数重复的条件表达式求和

2024-09-30 16:34:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个查询集。如何对jam=('16-17','17-18','18-19')的'output'求和

develop_queryset = InputCutSew.objects.filter(publish='2019-07-30').exclude(cell_name__isnull=True).exclude(cell_name__exact='').order_by('cell_name', 'jam').values('cell_name','model','jam').annotate\
            (total_output_ot=Sum(Case(When(jam='16-17', jam='17-18', jam='18-19', then='output')))).exclude(total_output_ot__isnull=True)

结果: SyntaxError:关键字参数重复

我已经试过了,但是输出还是分开不求和

.annotate(total_output_ot=Sum(Case(
                                      When(jam='16-17', then='output'),
                                      When(jam='17-18', then='output'),
                                      When(jam='18-19', then='output'),))).exclude(total_output_ot__isnull=True)
<QuerySet [{'cell_name': '13a', 'model': 'superstar', 'jam': '16-17', 'total_output_ot': 10}, {'cell_name': '13a', 'model': 'questar flow', 'jam': '16-17', 'total_output_ot': 20}, {'cell_name': '13a', 'model': 'superstar', 'jam': '17-18', 'total_output_ot': 20}, {'cell_name': '13a', 'model': 'questar flow', 'jam': '17-18', 'total_output_ot': 10}, {'cell_name': '13a', 'model': 'superstar', 'jam': '18-19', 'total_output_ot': 10}, {'cell_name': '13a', 'model': 'questar flow', 'jam': '18-19', 'total_output_ot': 20}, {'cell_name': '13b', 'model': 'superstar', 'jam': '16-17', 'total_output_ot': 10}, {'cell_name': '13b', 'model': 'questar flow', 'jam': '16-17', 'total_output_ot': 20}, {'cell_name': '13b', 'model': 'questar flow', 'jam': '17-18', 'total_output_ot': 10}, {'cell_name': '13b', 'model': 'superstar', 'jam': '17-18', 'total_output_ot': 20}, {'cell_name': '13b', 'model': 'questar flow', 'jam': '18-19', 'total_output_ot': 20}, {'cell_name': '13b', 'model': 'superstar', 'jam': '18-19', 'total_output_ot': 10}]>


Tags: nametrueoutputmodelcellotflowexclude
1条回答
网友
1楼 · 发布于 2024-09-30 16:34:30

也许你应该这样做:

develop_queryset = InputCutSew.objects.filter(publish='2019-07-30').exclude(cell_name__isnull=True).exclude(cell_name__exact='').order_by('cell_name', 'jam').values('cell_name','model','jam').annotate(
    total_output_ot=Case(
        When(jam='16-17', then=Value(17)),
        When(jam='17-18', then=Value(18)),
        When(jam='18-19', then=Value(19)),
    ), output_field=IntegerField()
).exclude(total_output_ot__isnull=True)


sum_of_total_output_ot = develop_queryset.aggregate(val = Sum('total_output_ot')).get('val')

相关问题 更多 >