如何在 self.env odoo 中传递 id?

How to pass id in self.env odoo?

在'products.template' table中我创建了三个字段width、length和gsm。现在我想在 'mrp' table.First 中检索它,我将从 mrp 物料清单中获取 ID,并将其分配给一个名为 prod 的变量。 'mrp.bom.line' table 通过迭代器包含产品 id.So 我想传递存储在 mrp 物料清单 table 中的产品 ID 以检索存储在 product.template table.I 收到错误 programming error can't adapt type 'product.product'

@api.multi
def _compute_rim_weight(self):
    bill_of_materials_id=[1,2,3]
    prod = self.env['mrp.bom.line'].browse(bill_of_materials_id)
    for i in prod:
            j = self.env['product.template'].browse(i.product_id)
            self.rim_weight = (j.width * j.length * j.gsm)/20000        
    return self.rim_weight

In ODOO browse take id not object

所以只需将 browse(i.product_id) 替换为 browse(i.product_id.id) 作为以下:

j = self.env['product.template'].browse(i.product_id.id)

还有一件事,如果 product.templatemodel :mrp.bom.line 有** many2one 关系**,根据我的理解,您甚至不需要调用 browse 。

直接调用line.product_id.width,line.product_id.length,line.product_id.gsm 如下:

@api.multi
def _compute_rim_weight(self):
    bill_of_materials_ids=[1,2,3]
    bom_lines = self.env['mrp.bom.line'].browse(bill_of_materials_ids)
    for line in bom_lines:            
            self.rim_weight = (line.product_id.width * line.product_id.length * line.product_id.gsm)/20000        
    return self.rim_weight


@api.one
def _compute_rim_weight(self):
    rim_weight   =0
    for line in self.bom_id.bom_line_ids:            
            rim_weight+ = (line.product_id.width * line.product_id.length * line.product_id.gsm)/20000        
    self.rim_weight =rim_weight