python - 如何覆盖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 {}
用于测试目的的打印,我该怎么做?
解决方案
推荐阅读
- ruby - 如何从 Ruby ERB 脚本访问作为 bash 数组的环境变量?
- node.js - 设置没有操作读取的文档?
- python - (Django)在视图中显示多个生成的表单(未知数量),并检索它们的结果?
- azure - 如何限制 eventthub 对特定消费者组的访问
- java - 如何在 spring webflux/webclient 中有条件地链接 webclient 调用
- flutter - 手势检测器不想将自己调整为子容器(除非你给它一个颜色)
- android - Gradle 依赖项下载罐子
- mysql - mysql 将当前用户的信息插入到表中
- python - 在 python 中使用“where”更改数据框中的值
- html - 表 td 没有崩溃