在下面的代码中,我得到了2行。然后我通过使用结果创建对象。然后,我将创建的值分配给one2many字段(inventory_line)。但是这里只显示了一行。我想列出在one2many中创建的所有vales。我该如何解决此问题??
@api.multi
def _inventory(self):
result = {}
if not self: return result
print ("Trueeeeeeeeeeeeee")
inventory_obj = self.env['tpt.product.inventory']
print (inventory_obj,"inventory_obj")
for id in self:
print (id,"id")
result.setdefault(id, [])
sql = 'delete from tpt_product_inventory where product_id=%s'%(id.id)
print (sql,"sql")
self._cr.execute(sql)
sql = '''
select foo.loc,foo.prodlot_id,foo.id as uom,sum(foo.product_qty) as ton_sl, foo.product_id from
(select l2.id as loc,st.prodlot_id,pu.id,st.product_qty,st.product_id
from stock_move st
inner join stock_location l2 on st.location_dest_id= l2.id
inner join product_uom pu on st.product_uom = pu.id
where st.state='done' and st.product_id=%s and l2.usage = 'internal'
union all
select l1.id as loc,st.prodlot_id,pu.id,st.product_qty*-1, st.product_id
from stock_move st
inner join stock_location l1 on st.location_id= l1.id
inner join product_uom pu on st.product_uom = pu.id
where st.state='done' and st.product_id=%s and l1.usage = 'internal'
)foo
group by foo.loc,foo.prodlot_id,foo.id, foo.product_id
'''%(id.id,id.id)
self._cr.execute(sql)
})
for inventory in self._cr.dictfetchall():
print (inventory,"inventory")
new_id = inventory_obj.create( {'warehouse_id':inventory['loc'],'product_id':inventory['product_id'],'prodlot_id':inventory['prodlot_id'],'hand_quantity':inventory['ton_sl'],'uom_id':inventory['uom']})
print (new_id,"new_id")
self.inventory_line = new_id
答案 0 :(得分:0)
我认为您最好为此场景创建一个SQL View并将其与模型tpt.product.inventory
关联,然后可以删除用于匹配记录的所有代码(删除并创建新代码)< / p>
您可以在这里找到一个非常相似的示例: