如何在many2many字段上使用write()方法?

Rai*_*han 0 odoo odoo-12

〜...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)})
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我试图做的是更新浏览模型(res)中的合作伙伴,但是名为partner_id的字段是一个many2many字段,我们可以在其中选择多个合作伙伴。

ker*_*ose 6

请注意,这仅适用于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). Needs to be a tuple, thus the comma.
(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
Run Code Online (Sandbox Code Playgroud)

但就你而言,它可能应该many2one是

(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). Needs to be a tuple, thus the comma.
(6, 0, [IDs])          replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
Run Code Online (Sandbox Code Playgroud)