使用 Onchange 更新 one2many 字段奥多14

问题描述

由于未保存字段“tax_ids_after_fiscal_position”的问题,我尝试使用在 pos.order.line 模型中调用的以下函数更新 One2many 字段 这是我的代码

class PosOrderLine(models.Model):
    _inherit = "pos.order.line" 

    @api.onchange('product_id')
    def _onchange_product_id(self):
        self.tax_ids_after_fiscal_position = self.product_id.taxes_id
        self.write({'tax_ids_after_fiscal_position': [(4,self.product_id.taxes_id)]})

我收到以下错误

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/http.py",line 639,in _handle_exception
    return super(JsonRequest,self)._handle_exception(exception)
  File "/usr/lib/python3/dist-packages/odoo/http.py",line 315,in _handle_exception
    raise exception.with_traceback(None) from new_cause
AttributeError: 'account.tax' object has no attribute 'ref'

请问怎么了? 有什么帮助吗? 谢谢。

解决方法

请不要在 onchange 内使用 write。

@api.onchange('product_id')
def _onchange_product_id(self):
    self.tax_ids_after_fiscal_position =  [(6,self.product_id.taxes_id.ids)]