【问题标题】:Update one2many field with Onchange . Odoo 14使用 Onchange 更新 one2many 字段。奥多 14
【发布时间】:2021-07-21 05:58:46
【问题描述】:

由于未保存字段“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'

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

【问题讨论】:

  • 不要使用 Odoo。如果您真的想做一些不涉及太多编码的事情,请使用 Bubble。我曾经专业地使用这些东西。很垃圾。
  • 您可以发布文件吗?这似乎不是错误的来源。
  • @Juliana Hill ,哪个文件?
  • 它看起来可能与:self.write({'tax_ids_after_fiscal_position': [(4, self.product_id.taxes_id)]}) 我不太确定我是否完全记得这一点,但我认为元组的数字值是错误的。

标签: python oop inheritance orm odoo


【解决方案1】:

请不要在 onchange 中使用 write。

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

【讨论】:

  • @K.ju 你试过了吗?
  • 我试过这个解决方案,保存时总是出现问题,我也尝试添加 store = True ,但这并不能解决问题。 ;字段 tax_ids_after_fiscal_position 是一个计算字段,我不知道在数据库中存储字段时这是否会导致问题...... @Juliana Hill
  • @K.ju 这就是为什么!您无法使用 Odoo 保存在计算域之上。您必须重新定义数据库中的字段
  • 我曾经有代码尝试过这些概念,但我不太确定这些存储库在哪里......可能在 Odoo 的旧过期帐户上,哈哈
  • 完成您所要求的唯一方法是通过在继承模型的同时覆盖相应的函数来重新计算计算域
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
  • 2021-06-29
  • 1970-01-01
相关资源
最近更新 更多