Odoo重写函数调用ord

2024-04-27 03:55:40 发布

您现在位置:Python中文网/ 问答频道 /正文

据我所知,odoo并不像Python扩展其类(_inherit = 'model')那样扩展其模型。这似乎很合理。我的问题是:

如果我的customModule1扩展了sale.order并重写了write方法,添加了一些功能,然后我安装了customModule2,它反过来扩展了sale.order模型,并覆盖了添加了一些功能的write方法的所有版本,那么我知道将调用{}方法的所有版本,但顺序是什么?在

当客户机在sale.order模型上写操作时,customModule1write会首先被调用吗?还是writecustomModule2?在


Tags: 方法odoo模型功能版本客户机model顺序
2条回答

是的,这是非常有趣的一点,没有人能够预测哪个模块首先调用哪个方法,因为odoo管理依赖的层次结构。在

Calling pattern comes into the picture only when the method will be called from object (manually from code) and if write method call from UI (means Sales Order edit from UI) then it will call each write method written for that model no matter in which module it is and it's sequence is LAST WRITTEN CALL FIRST (but it's only when the method is called from UI).

因此,在您的例子中,自定义模块1自定义模块2将处于同一级别,并且都具有相同的父级销售订单。在

销售订单=>自定义模块1(写入方法重写)

销售订单=>自定义模块2(写入方法重写)

So while the write method will be called manually from code then it will gives priority to local module first and then it will call super method.

在这种情况下,假设write方法从模块1调用,那么它可能会忽略模块2的write方法,因为模块1和模块2位于同一级别(super称为父类的write方法)。因为我们在开发过程中多次遇到这样的问题:在多个模块上重写的方法,并且这些方法在同一级别上,那么它将不会调用下一个模块的方法。在

所以,当您需要调用每个模块的每个方法时,它们必须在层次结构中,但不能在同一级别上。在

因为有一个主要的原因是,对于并行模块,这个方法有时不会被调用。在

因为这里有两件事

(一)。依赖于:父模块(决定模块层次结构)

(二)。继承:这里定义了对象的方法和行为。在

Module 1 and Module 2 are not there in depends of each other so by hierarchy it's not necessary to call the method from these both module no matter whether they are overriding the same method of same model.

write of last installed module will call first.

customModule2write将首先调用(因为它是最后安装的),如果您在write中调用了super,那么将根据super position调用customModule1的write。在

@api.multi
def write(self, vals):       
    res = super(product_product, self).write(vals)
    # do your work   after super call 
    return res

或者

^{pr2}$

相关问题 更多 >