odoo - 如何在 many2many 字段上使用 write() 方法?
问题描述
~...py
@api.onchange('test_record')
def abcde(self):
rec = self.test_record.id
res = self.env['anc'].browse(rec)
res.write({'partner_id': (4,self.partner_id.id)})
在上面的代码中,我试图做的是更新浏览模型中的合作伙伴(res),但是名为 partner_id 的字段是一个 many2many 字段,我们可以在其中选择多个合作伙伴。
解决方案
请注意,这仅适用于many2many
或one2many
如下:
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write *values* on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5) unlink all (like using (3,ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
但在你的情况下,它可能many2one
应该是
def abcde(self):
rec = self.test_record.id
res = self.env['anc'].browse(rec)
res.write({'partner_id':[(4,self.partner_id.id)]}) # you need to add it as list
推荐阅读
- python - 为什么python程序不显示结果
- intellij-idea - Datanucleus Enhancer:IntelliJ:插件问题
- swift - 不安全可变指针
vDSP_fft_zip 返回的立即被覆盖 - vba - 运行命令未显示 VBA 脚本是否需要将任何参数传递给它
- genexus - GeneXus 的 .open 方法返回什么数值
- sql - 查询以收集字符串中字符的最后一次出现 (SQL Progress OpenEdge)
- sql - 从父节点xml sql server检索所有子节点
- angular - Angular 可以像 AngularJS 一样与任何 html 支持技术一起使用吗
- powershell - 注册表路径上的测试路径返回不同的值
- git - git:仅跟踪由数千个小文件组成的大量 repo