了解 Odoo 中的 "takes at least X arguments (X given)" 错误

Understanding "takes at least X arguments (X given)" error in Odoo

我在 stock.picking 模型的方法中有以下代码:

...
for line in picking.move_lines:
    if line.procurement_id and line.procurement_id.sale_line_id:
        sale_line = line.procurement_id.sale_line_id
        _logger.info(sale_line)
        cur = sale_line.order_id.pricelist_id.currency_id
        price = sale_line._calc_line_base_price()
        qty = sale_line._calc_line_quantity()
        ...

我正在检查记录器 sale_line 变量是一个 sale.order.line 对象 - 我得到 sale.order.line(14,)- ,所以我不明白为什么会出现以下错误:

TypeError: _calc_line_base_price() takes at least 4 arguments (4 given)

如果我用旧的 API 格式调用该方法,它可以工作,但是如何用新的 API 格式调用它?

这个答案很好地解释了问题,但我认为这不是我的情况,因为我调用的方法在 sale.order.line:

的 class 中

谁能给我解释一下?顺便说一下,我正在使用版本 8。

在新式方法中调用旧式方法时,Odoo 会尝试正确包装参数。这里的问题是方法 _calc_line_base_price 因为它不能自动换行。

def _calc_line_base_price(self, cr, uid, line, context=None):

参数 line 对于这种类型的方法来说是不正常的(调用一个实例,新的 api 将是 @api.multiself.ensure_one())。所以你需要像 "static" odoo class 方法一样调用它:

price = self.env['sale.order.line']._calc_line_base_price(sale_line)