Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How do you create and update One2Many and Many2Many records with Python 3?

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])
 
PREVIOUS NEXT
Tagged: #How #create #update #records #Python
ADD COMMENT
Topic
Name
5+6 =