如何将djang中具有相同值的多行分组到一行中

2024-04-25 01:54:37 发布

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

    {'item__sale_code': None, 'order__customer__code': 'CLC', 'outstanding_quantity': '5.00', 'order__currency__name': 'YEN', 'original_amount': '92500.00', 'order__customer__name': 'Cloud Customer', 'local_amount': Decimal('0')}
    {'item__sale_code': 'None', 'order__customer__code': 'CLC', 'outstanding_quantity': '3.00', 'order__currency__name': 'YEN', 'original_amount': '36000.00', 'order__customer__name': 'Cloud Customer', 'local_amount': Decimal('0')}
{'item__sale_code': 'None', 'order__customer__code': 'SGC', 'outstanding_quantity': '10.00', 'order__currency__name': 'YEN', 'original_amount': '66000.00', 'order__customer__name': 'SCG Customer', 'local_amount': Decimal('0')}

我在django有一个元组列表。现在,我想将具有相同客户的两行分组为一行,并计算outstanding_quantityoriginal_amount之和。应该是这样的:

{'item__sale_code': 'None', 'order__customer__code': 'CLC', 'outstanding_quantity': '8.00', 'order__currency__name': 'YEN', 'original_amount': '128500.00', 'order__customer__name': 'Cloud Customer', 'local_amount': Decimal('0')}
{'item__sale_code': 'None', 'order__customer__code': 'SGC', 'outstanding_quantity': '10.00', 'order__currency__name': 'YEN', 'original_amount': '66000.00', 'order__customer__name': 'SCG Customer', 'local_amount': Decimal('0')}

这是我的代码python.py:

my_list = my.values('order__customer__code', 'order__customer__name', 'order__currency__name',
                                         'item__sale_code') \
            .annotate(Sum('outstanding_quantity')) \
            .annotate(Sum('original_amount')) 

Tags: namenonelocalcodeordercustomersaleitem