python - 字典合并列表

标签 python odoo

我在这里想做的是“合并”具有相同product_id的相似行,并将其附加到另一个 View 向导中获取one2many字段

我必须使用此方法在向导字段中填写 sale_order_line

def default_get(self, cr, uid, fields, context=None):
    if context is None: context = {}
    res = super(sale_order_consolidation, self).default_get(cr, uid, fields, context=context)
    picking_ids = context.get('active_ids', [])
    active_model = context.get('active_model')

    if not picking_ids or len(picking_ids) != 1:

        return res
    assert active_model in ('sale.order'),
    picking_id, = picking_ids
    picking = self.pool.get('sale.order').browse(cr, uid, picking_id, context=context)
    items = []
    packs = []
    if not picking.order_line:
        picking.do_prepare_partial()
    for op in picking.order_line:
        item = {
        'ref': op.ref,
        'product_id': op.product_id.id,
        'name': op.name,
        'product_uom_qty': op.product_uom_qty,
        'product_uom': op.product_uom.name,
        'price_standard': op.price_standard,
        'price_unit': op.price_unit,
        'discount_2': op.discount_2,
        #'tax_id': op.tax_id.id,
        'price_subtotal': op.price_subtotal, 
        }
        #if op.product_id:
        items.append(item)

    for rec in items:
        key = d['product_id']
       # the right instruction to add QTY with the same products and append it to packs in order to update my one2many field

    res.update(order_line_consolidation=items)

    return res

我真正想要的是:

d = [{'product_id': "x" , 'price': 1 , 'Qty': 2},
     {'product_id': "y" , 'price': 5 , 'Qty': 4},
     {'product_id': "x" , 'price': 1 , 'Qty': 1},
     {'product_id': "z" , 'price': 9 , 'Qty': 1},
     {'product_id': "y" , 'price': 5 , 'Qty': 5}
    ]

结果:

d = [{'product_id': "x" , 'price': 1 , 'Qty': 3},
     {'product_id': "y" , 'price': 5 , 'Qty': 9},
     {'product_id': "z" , 'price': 9 , 'Qty': 1},
    ]

我真正寻找的是添加相同产品的数量并将其附加到包中以更新我的 one2many 字段的正确方法

感谢您的帮助,谨致问候。

最佳答案

不要使用列表,而是使用字典并将product_id作为键:

    items = {}
    for op in picking.order_line:
            item = items.get(op.product_id.id, False)
            if item:
                # if we have an item with the same product_id
                # just add the quantity
                item['product_uom_qty'] += op.product.product_uom_qty
            else:
                # create new dictionary and add it to glocal dictionary
                item = {
                    'ref': op.ref,
                    'product_id': op.product_id.id,
                    'name': op.name,
                    'product_uom_qty': op.product_uom_qty,
                    'product_uom': op.product_uom.name,
                    'price_standard': op.price_standard,
                    'price_unit': op.price_unit,
                    'discount_2': op.discount_2,
                    #'tax_id': op.tax_id.id,
                    'price_subtotal': op.price_subtotal, 
                }
                #add in product.id key
                items[op.product_id.id] = item

    # in the end convert the dict to list
    list_item = []
    for dummy, item in items.iteritems():
        list_item.append(item)

关于python - 字典合并列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517038/

相关文章:

python - 用形状为 (x, y) 的二维 bool 掩码掩蔽形状为 (x, y, z) 的 3 维张量

python - pandas 数据框中的 if-else 语句

python - 我的 python 代码中没有任何内容被附加(写入)到我的 txt 文件中

python - 上下文值未传递到具有 onchange 属性的字段

python - 在 django 模型中使用 Greatest 函数时如何获取注释中相关对象的列表

python - 列表的逻辑索引

ruby-on-rails - 哪些 ERP 具有像样的 Ruby 连接器或完整的 API?

python - 奥多。按字段一对多搜索

python - 单元测试未在 Odoo 12 中运行(加载)

odoo - TreeView 中的颜色 Odoo 9 错误