在下面的代码中,我得到了一个2行的结果。然后使用结果创建对象。然后我将创建的值赋给one2many字段(inventory\u line)。但这里只显示了一行。我想列出所有创建的值在一个2任何。。?如何解决此问题
@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
我认为最好为这个场景创建一个SQL视图,并将它与模型
tpt.product.inventory
关联起来,然后删除所有用于匹配记录的代码(删除并创建新的记录)你可以在这里找到一个非常相似的例子:
https://github.com/odoo/odoo/blob/695050dd10e786d7b316f6e7e40418441cf0c8dd/addons/stock/report/report_stock_forecast.py
相关问题 更多 >
编程相关推荐