OCA / odoorpc

Python module to pilot your Odoo servers through JSON-RPC.
http://pythonhosted.org/OdooRPC/
GNU Lesser General Public License v3.0
237 stars 125 forks source link

Check if Many2many field contains a value #47

Closed agrings closed 5 years ago

agrings commented 5 years ago

Hello,

I'm trying to include a Partner in a category, but first I need to verify when it is in the category already. Something like this:

Partner=odoo.env['res.partner']
partner=Partner.browse(1)
if 20 not in partner.category_id:
   partner.category_id+=[20]

But the m2m field doesn't work as a list for the "in" operator.

pedrobaeza commented 5 years ago

AFAIK, it should be if 20 not in partner.category_id.ids:

sebalix commented 5 years ago

@pedrobaeza is right, you can also write this:

categ = odoo.env['category.model'].browse(20)
if categ not in partner.category_id:
    partner.category_id += categ

Does it solve your issue?

agrings commented 5 years ago

Perfect @sebalix and @pedrobaeza . Thank you!