词典合并列表 [英] list of dictionary consolidation

查看:79
本文介绍了词典合并列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在此处执行的操作是合并"具有相同product_id的相似行,并将其附加到另一个视图wizard

what i'm trying to do here is "consolidate" similar lines with the same product_id,and append it to get one2many field in another view wizard

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

I got to fill a sale_order_line in my wizard field uing this method;

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},
    ]

我真正想要的是添加具有相同产品的数量并添加到包装中以更新我的一个很多字段的正确方法

what I actually looking for is the right way to add QTY with the same products and append it to packs in order to update my one2many field

谢谢您的帮助,最诚挚的问候.

thanks you for your help, Best regards.

推荐答案

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

instead of using list use dictionary and make the product_id as key:

    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)

这篇关于词典合并列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆