## @api.one
新api定義:
1 qty = fields.Integer(string="qty", compute="_get_qty") 2 3 @api.one 4 def _get_qty(self): 5 self.qty = random.randint(1, 10)
one一般使用再無返回值,就算添加return也不會返回,也不會報錯,版本10以后逐漸淡化
## model
新api定義
@api.model
def create(self, vals): bom_obj = self.search([("product_tmpl_id", '=', vals.get('product_tmpl_id', None))]) if bom_obj: raise UserError('該產品已存在相應的物料清單') return super(MrpBom, self).create(vals)
注意:這個方法的注釋是這樣的:修飾記錄樣式的方法,其中``self``是記錄集,但和內容無關緊要,只有模型相關
假如上面使用mutil裝飾就會出錯,提示參數個數的報錯, 此方法可以有返回值
def model(method): """ Decorate a record-style method where ``self`` is a recordset, but its contents is not relevant, only the model is. Such a method:: @api.model def method(self, args): ... may be called in both record and traditional styles, like:: # recs = model.browse(cr, uid, ids, context) recs.method(args) model.method(cr, uid, args, context=context) Notice that no ``ids`` are passed to the method in the traditional style. """ method._api = 'model' return method
## mutil
新api定義
@api.multi def search_recursively_line_product(self): product_ids = set() for rec in self: for bom_line in rec.bom_line_ids: product_ids.add(bom_line.product_id.id) product_tmpl_id = self.env['product.product'].browse(bom_line.product_id.id).product_tmpl_id.id bom_line_bom = self.search([('product_tmpl_id', '=', product_tmpl_id)]) if bom_line_bom.bom_line_ids: product_ids |= bom_line_bom.search_recursively_line_product() return product_ids
注:mutil和one時對應的,可以返回一個或者回個記錄集
修飾一個記錄樣式的方法,其中``self``是一個記錄集,通常定義對記錄的操作
def multi(method): """ Decorate a record-style method where ``self`` is a recordset. The method typically defines an operation on records. Such a method:: @api.multi def method(self, args): ... may be called in both record and traditional styles, like:: # recs = model.browse(cr, uid, ids, context) recs.method(args) model.method(cr, uid, ids, args, context=context) """ method._api = 'multi' return method