OpenERP商品添加的时候同类商品自动合并方法优化


        通过这次尝试,深入理解了python字典的用法。

        在库存入库商品添加的时候,同样的商品我的处理方法(Methon One)和老大指点的优化方法(Two),效率不是一点半点的高:

def create(self, cr, uid, vals, context=None):
        if not vals.has_key('move_lines_apply') or len(vals['move_lines_apply'])==0:
            raise except_osv(_('Warning'),_("请添加商品后再提交"))

        #Methon One
        new_move_lines = {}
        move_lines_total = {}
        for move_line in vals['move_lines_apply']:
            key = move_line[2]['product_id']
            value = move_line[2]['product_qty']
            new_value = move_lines_total.get(key,0) + value
            move_lines_total[key] = new_value
            move_line[2]['product_qty'] = new_value
            new_move_lines[key] = move_line

        #Methon Two
        move_lines_total = {}
        for move_lines in vals['move_lines_apply']:
            key = move_lines[2]['product_id']
            value = move_lines[2]['product_qty']
            move_lines_total[key] = move_lines_total.get(key,0) + value
        
        new_move_lines = []
        for line in move_lines_total:
            num = move_lines_total[line]
            for move_lines in vals['move_lines_apply']:
                if line == move_lines[2]['product_id']:
                    move_lines[2]['product_qty'] = num
                    new_move_lines.append(move_lines)
                    break

        vals['move_lines_apply'] = new_move_lines.values()
        vals['state'] = 'wait_picking'
        return super(stock_picking_apply, self).create(cr, uid, vals, context=context)


你可能感兴趣的:(python)