Search code examples
odooone-to-manyxml-rpcmany-to-oneodoo-10

Odoo 10 XMLRPC How to map one2many and many2one


I have recently been doing some development in python 2.7 with Odoo 10 API using XMLRPC. My questions are:

  1. How do I write a one2many field to a field in odoo via xmlrpc

  2. How do u write a many2one field to a field in odoo via xmlrpc

Many thanks your help is much appreciated!

Samuel


Solution

  • For Many2one fields you can simply use the ID of the record:

    my_partner_id = 1  # or use a search to find the correct one
    id = models.execute_kw(db, uid, password, 'sale.order', 'create', [{
        'partner_id': my_partner_id,
    }])
    

    Many2many or One2many fields are a bit special. There are some magic triplets in Odoo, you have to use with such fields -> Model Reference/CRUD/write(vals).

    For example if you want to add a tag to a customer (Many2many field):

    my_tag_id = 42  # or use a search to find the correct one
    id = models.execute_kw(db, uid, password, 'res.partner', 'write',
        [my_partner_id], [{
        'category_id': [(4, my_tag_id)],
    }])
    

    Or if you want to delete all tags:

    my_tag_id = 42  # or use a search to find the correct one
    id = models.execute_kw(db, uid, password, 'res.partner', 'write',
        [my_partner_id], [{
        'category_id': [(5,)],
    }])
    

    Or if you want to substitute all tags by some others:

    my_tag_id1 = 42  # or use a search to find the correct one
    my_tag_id2 = 7  # or use a search to find the correct one
    id = models.execute_kw(db, uid, password, 'res.partner', 'write',
        [my_partner_id], [{
        'category_id': [(6, None, [my_tag_id1, my_tag_id2])],
    }])