如何在 many2many 字段上使用 write() 方法? [英] How to use write() Method on a many2many field?

查看:52
本文介绍了如何在 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字段,我们可以在其中选择多个合作伙伴.

On the above code what im trying to do is updating a partner in the browsed model(res),But the field named partner_id is a many2many field,where we can select multiple partners.

推荐答案

请注意,这仅适用于 many2manyone2many 如下:

please note this is only for many2many or one2many as following:

(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 应该是

but in your case it is probably many2one which should be as

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

这篇关于如何在 many2many 字段上使用 write() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆