OpenERP 疑问之一

def _get_send_amount(self,cr,uid,ids,name,args,context=None):
        res={}
        MRP={}
        lines = self.browse(cr,uid,ids)
        #odd code:here the code will execute towice,first time execute by line id asc
        #but second time it will execute by line id desc
        #so,I have to sort it before the loop.
        lines.sort(key=lambda x:x.id)
        for line in lines:
	    result=self._get_send_data(cr,uid,line.id,name,args,context=context)
	    if result["amount"] > line.amount:
		if not MRP.has_key(line.product_id.id):
		    MRP[line.product_id.id]=result["amount"]-line.amount
		    res[line.id]=line.amount
		elif MRP[line.product_id.id]-line.amount>0:
		    res[line.id]=line.amount
		    MRP[line.product_id.id] -=line.amount
		else:
		    res[line.id]=MRP[line.product_id.id]
		    MRP[line.product_id.id]=0
	    else:
		res[line.id]=result["amount"]
	    
	for line2 in lines:
	    #产品是否是组合产品
	    mrp = self.pool.get('mrp.bom').search(cr,uid,[('product_id','=',line2.product_id.id),('bom_id','=',False)],context=context)
	    if len(mrp)>0:
		min_count = 0
		bom = self.pool.get('mrp.bom').browse(cr,uid,mrp[0],context=context)   
		res_count=[]
		#res_orders=[]
		i=1
		for bom_line in bom.bom_lines:
		    res_count.append(int(res[line2.id+i] / bom_line.product_qty))
		    i+=1
		if len(res_count)>0: 
		    amount =min(res_count)
		else:
		    amount =0
		res[line2.id]=amount	
        return res

我在迭代循环中发现,自定义方法_get_send_amount会执行两次,而且第一次执行的顺序是正序,第二次执行的顺序是倒序。

不知到是为什么。。

原文地址:https://www.cnblogs.com/kfx2007/p/3741214.html