• odoo api装饰器 one、model和multi区别


    ## @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
  • 相关阅读:
    1833: [ZJOI2010]count 数字计数——数位dp
    【模板】BZOJ 3685: 普通van Emde Boas树——Treap
    【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法
    BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
    【模板】二分图匹配/一般图匹配——匈牙利算法/随机匈牙利算法
    【模板】BZOJ 1692:队列变换—后缀数组 Suffix Array
    BZOJ 4241: 历史研究——莫队 二叉堆
    【模板】BZOJ 3781: 小B的询问 莫队算法
    BZOJ 3656: 异或 (组合数取模 CRT)
    【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
  • 原文地址:https://www.cnblogs.com/alexzu/p/13043611.html
Copyright © 2020-2023  润新知