码迷,mamicode.com
首页 > Windows程序 > 详细

odoo开发中@api.depends与@api.onchange的区别

时间:2020-06-11 16:24:15      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:precision   ima   lan   就是   pac   text   一个   get   ack   

@api.depends

这个装饰器主要运用在odoo的字段相关的函数中。在函数中,对该字段值进行处理时,如果计算值依赖于其他相关字段,不论是与该字段隶属同一张表的还是其他表的字段,都会触发所编写的字段函数。正因为此,可以利用@api.depends 装饰来侦测与字段相关的其他表的字段。如果decorator中依赖的任何字段“被ORM更改或在表单中更改”,都将触发对decorated函数的调用。

@api.onchange

这个装饰器触发decorated函数的时机是:定义的指定字段如果在form窗体出现了变化,就会触发执行。这里要注意的是,这些字段是隶属于同一个模型或同一界面。

综上,虽然 @api.depends 与@api.onchange有诸多相似的地方,但一个重要区别就是 onchange 的侦测字段范围需要在同一个模型或界面上;而depends 可以侦测关联的模型的字段(可以跨不同表)。

 

    @api.onchange(product_uom_qty, product_uom, route_id)
    def _onchange_product_id_check_availability(self):
        if not self.product_id or not self.product_uom_qty or not self.product_uom:
            self.product_packaging = False
            return {}
        if self.product_id.type == product:
            precision = self.env[decimal.precision].precision_get(Product Unit of Measure)
            product = self.product_id.with_context(
                warehouse=self.order_id.warehouse_id.id,
                lang=self.order_id.partner_id.lang or self.env.user.lang or en_US
            )

上面这段代码节选自sale_order.py 文件SaleOrderLine类中, ‘product_uom_qty‘, ‘product_uom‘, ‘route_id‘ 这些字段都是定义在 sale.order.line 模型中的。

再看下面代码中用到的depends:

class SaleOrderLine(models.Model):
    _inherit = sale.order.line

    qty_delivered_method = fields.Selection(selection_add=[(stock_move, Stock Moves)])
    product_packaging = fields.Many2one(product.packaging, string=Package, default=False)
    route_id = fields.Many2one(stock.location.route, string=Route, domain=[(sale_selectable, =, True)], ondelete=restrict)
    move_ids = fields.One2many(stock.move, sale_line_id, string=Stock Moves)

    @api.multi
    @api.depends(product_id)
    def _compute_qty_delivered_method(self):
        """ Stock module compute delivered qty for product [(‘type‘, ‘in‘, [‘consu‘, ‘product‘])]
            For SO line coming from expense, no picking should be generate: we don‘t manage stock for
            thoses lines, even if the product is a storable.
        """
        super(SaleOrderLine, self)._compute_qty_delivered_method()

        for line in self:
            if not line.is_expense and line.product_id.type in [consu, product]:
                line.qty_delivered_method = stock_move

    @api.multi
    @api.depends(move_ids.state, move_ids.scrapped, move_ids.product_uom_qty, move_ids.product_uom)
    def _compute_qty_delivered(self):
        super(SaleOrderLine, self)._compute_qty_delivered()

这段代码与上面onchange片段一样都来自SaleOrderLine类,但可以看到 @api.depends(‘product_id‘) 中的product_id字段是不在sale.order.line模型中的。同样,@api.depends(‘move_ids.state‘, ‘move_ids.scrapped‘, ‘move_ids.product_uom_qty‘, ‘move_ids.product_uom‘) 也是跨表侦测字段。

从上面两段代码,就能看出两者最主要的区别,另外从触发来看,api.onchage在字段变化的时候会触发一次,当字段内容变换完成保存的时候还会触发一次。而api.depends则只在字段变化的时候触发一次。

 

odoo开发中@api.depends与@api.onchange的区别

标签:precision   ima   lan   就是   pac   text   一个   get   ack   

原文地址:https://www.cnblogs.com/excelserver/p/13092388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!