This question has been flagged
2 Replies
8977 Views

I'm working with the xmlrpc api using Python 3.
I'm having some trouble understanding the syntax for creating or updating a one2many or many2many type.
I've figured out the basic create and read commands for generic data types, but I can't seem to wrap my head around the documentation for the others, nor can I find much external help on the matter.

Avatar
Discard
Best Answer

Here is a code snippet that will create a new Partner, a new Contact, then update the Contact.

import xmlrpc.client

#connection details
server_url = 'http://localhost:8069'
db = 'database_name'
login = 'admin'
password = 'admin'

#endpoints
common_ep = xmlrpc.client.ServerProxy(server_url+'/xmlrpc/2/common')
object_ep = xmlrpc.client.ServerProxy(server_url+'/xmlrpc/2/object')

#login
uid = common_ep.authenticate(db, login, password,{})

#define new Partner
new_partner = {
'name': 'Joe Smith',
'ref': '12345',
}

#create New Partner
new_partner_id = object_ep.execute_kw(db, uid, password, 'res.partner', 'create', [new_partner])

#define New Contact
new_contact = {
'name': 'Jane Smith',
'ref': '54321',
'parent_id': new_partner_id,
}

#create New Contact, under New Partner
new_contact_id = object_ep.execute_kw(db, uid, password, 'res.partner', 'create', [new_contact])

#define update for New Contact

updated_information = {
'phone': '555 123 1234',
}

#find New Contact
existing_contact_id = object_ep.execute_kw(db, uid, password, 'res.partner', 'search', [[['ref','=','54321']]])

object_ep.execute_kw(db, uid, password, 'res.partner', 'write', [existing_contact_id, updated_information])
Avatar
Discard
Best Answer

There's documentation on the "write" method for the special syntax for these o2m and m2m fields: https://www.odoo.com/documentation/12.0/reference/orm.html#model-reference

However, I would suggest using a library to wrap the XML-RPC or JSON-RPC calls with a simpler syntax.

For example you can use either \Odooly or OdooRPC libs (available on PyPI and Github).

Sample code with odooly (doing the same stuff as the comment above):

 
import odooly

client = odooly.Client('http://localhost:8069/jsonrpc', 'database_name', 'admin', 'admin')

env = client.env

# define new Partner
partner_data = {
'name': 'Joe Smith',
'ref': '12345',
}

# create New Partner
new_partner = env['res.partner'].create(partner_data)

# define New Contact
contact_data = {
'name': 'Jane Smith',
'ref': '54321',
'parent_id': new_partner.id,
}

new_contact = env['res.partner'].create(contact_data)

updated_information = {
'phone': '555 123 1234',
}

#find New Contact
existing_contact = env['res.partner'].search(['ref = 54321'])

existing_contact.write(updated_information)

----


Avatar
Discard