首页 > 解决方案 > 如何覆盖odoo中字典列表中的重复键

问题描述

我正在处理 POS 订单的产品摘要报告,我制作了一个循环所有 pos 订单行并从那里检索数据的方法,最终结果应该是唯一的产品名称,但加上销售数量的总和,我做到了,但是输出返回非唯一产品,列表的长度是订单行的总和,我需要删除所有重复的产品并只设置最后一个具有最大数量的产品,这里是代码

@api.multi
def product_summary_test(self):
    product_summary_dict = {}
    data = []
    if self.date_from and self.date_to:
        order_detail = self.env['pos.order'].search([('date_order', '>=', self.date_from),
                                                     ('date_order', '<=', self.date_to)])
        if order_detail:
            for each_order in order_detail:
                for each_order_line in each_order.lines:
                    if each_order_line.product_id.name in product_summary_dict:
                        product_qty = product_summary_dict[each_order_line.product_id.name]
                        product_qty += each_order_line.qty
                        res1 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                        }
                        data.append(res1)
                    else:
                        product_qty = each_order_line.qty
                        res2 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                        }
                        data.append(res2)

                    product_summary_dict[each_order_line.product_id.name] = product_qty;
    if data:
        print(len(data))
        print(data)
        return data
    else:
        return {}

用于测试目的的打印,我该怎么做?

标签: pythonarrayspython-3.xodoo

解决方案


推荐阅读