Odoo中的Map
在Odoo源代码中,常常可以看到使用ORM的map。
Model.mapped(func)
Parameters参数:func (callable or str) – a function or a dot-separated sequence of field names
Returns返回:self if func is falsy, result of func applied to all self
records.
Return type返回类型:list or recordset
如下代码示例:
1 2 3 4 5 6 7 8 | # returns a list of names records.mapped( 'name' ) # returns a recordset of partners records.mapped( 'partner_id' ) # returns the union of all partner banks, with duplicates removed records.mapped( 'partner_id.bank_ids' ) |
1 2 | # returns a list of summing two fields for each record in the set records.mapped( lambda r: r.field1 + r.field2) |
如下面代码,使用map来统计订单行的产品成本:
1 @api.depends('order_line.product_uom_qty','order_line.purchase_price') 2 def _compute_product_cost(self): 3 for order in self: 4 if order.order_line: 5 order.total_product_cost = sum(order.order_line.mapped(lambda r: r.purchase_price * r.product_uom_qty))