Odooers论坛

欢迎!

该社区面向专业人士和我们产品和服务的爱好者。
分享和讨论最好的内容和新的营销理念,建立您的专业形象,一起成为更好的营销人员。


0

.write() behaviour on Many2Many - Odoo 10

形象
odoo
1 备注
形象
丢弃
形象
odoo
-

i haven't known your model and type of variable, so i can't fix your code

1 答案
0
形象
odoo
最佳答案

If the category_id is many2many field on partner master, then you can try the following code.

partner_ids = self.env['res.partner'].search([('id','=',partner_id)])

if partner_ids:

    category_ids = [contact_tag_id]+partner_ids.category_id.ids

    partner_ids.sudo().write({'category_id': [(6, 0, category_ids)]})

4 注释
形象
丢弃
形象
odoo
-

Thanks for your reply. See my edit to the question. My code is already working well from the start but how do I check if the ID that I am adding to category_id is already there before the write happens?

形象
odoo
-

partner_ids.category_id.ids

形象
odoo
-

So something like this?

partner_ids = self.env['res.partner'].search([('id','=',partner_id)])

if contact_tag_id not in partner_ids.category_id.ids

#Add the tag to contact

if contact_tag_id != None:

project_id = contact_tag_id

env['res.partner'].search([('id','=',partner_id)]).sudo().write({'category_id': [(4, contact_tag_id)]})

形象
odoo
-

yes your right