字典合并列表

时间:2017-11-27 17:55:29

标签: python openerp

我在这里尝试做的是"巩固"具有相同product_id的相似行,并将其附加到另一个视图中获取one2many字段wizard

我必须在我的向导字段中使用此方法填充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},
    ]

我真正想要的是用相同的产品添加QTY并将其附加到包以便更新我的one2many字段的正确方法

感谢您的帮助,祝贺您。

0 个答案:

没有答案