如何解决Odoo:更新 one2many 字段上的记录时触发 on_change?
如何在 one2many 字段上更新记录时触发 on_change?
解决方法
.. warning::
``@onchange`` only supports simple field names,dotted names
(fields of relational fields e.g. ``partner_id.tz``) are not
supported and will be ignored
.. danger::
Since ``@onchange`` returns a recordset of pseudo-records,calling any one of the CRUD methods
(:meth:`create`,:meth:`read`,:meth:`write`,:meth:`unlink`)
on the aforementioned recordset is undefined behaviour,as they potentially do not exist in the database yet.
Instead,simply set the record's field like shown in the example
above or call the :meth:`update` method.
.. warning::
It is not possible for a ``one2many`` or ``many2many`` field to modify
itself via onchange. This is a webclient limitation - see `#2693 <https://github.com/odoo/odoo/issues/2693>`_.
这是我看到@api.onchange的代码时得到的,只是不支持one2many
,这是可能的。 只需按照以下步骤操作:
-
创建一个 Many2one 字段。
-
创建一个 One2many 字段来存储 onchange 记录。
-
创建 onchange 方法。
代码:
many2one_field = fields.Many2one('One2manyFieldModelName',string="Many2one Field")
one2many_record_field = fields.One2many('Many2oneFieldModelName','field_name',store=True)
@api.onchange('many2one_field')
def _onchange_many2one_field(self):
self.one2many_record_field = self.env['One2manyFieldModelName'].search([('One2manyFieldName','=',self.many2one_field)])
此外,如果您使用的是 Odoo 版本 11 及更高版本,则必须在 XML 中提供属性 force_save 以将只读字段保存在 onchange 中。
<field name="field_name" force_save="1"/>
说明: 创建一个包含one2many字段数据的Many2one字段,然后创建一个one2many字段,根据需要存储onchange记录,然后调用onchange方法并使用搜索功能存储记录后,您可以使用搜索,浏览等任何功能。
我希望你能理解,如果不只是评论。
如果有用就接受答案...
谢谢:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。