top of page
  • Writer's pictureTek Siong, Hock

Tips to use XML-RPC for Odoo for Data migration

XML-RPC is one of the Odoo API to allow access to the Odoo database to create, write, search and delete records.

XML-RPC Use Cases

1) Mobile app development to access to Odoo, such as react-native and flutter development.

2) Migrate master data and transactional data from the Ms Excel to the Odoo, especially validation and customization are involved.

3) Third party python based web application to connect to Odoo.

a) If you want to run the XML-RPC from your local python IDE, import xmlrpc.client.

import xlrd
import xmlrpc.client

b) Make the connection to the Odoo database

username = ''
pwd = 'password'
dbname = "mydb"
server = 'http://IP:8069'

sock_common = xmlrpc.client.ServerProxy(server + '/xmlrpc/common', allow_none=True)
sock = xmlrpc.client.ServerProxy(server + '/xmlrpc/object', allow_none=True)
uid = sock_common.login(dbname, username, pwd)

c) Search for the record in Odoo

This search res.users model and will return the user id if found

user_id = sock.execute(dbname, uid, pwd, 'res.users', 'search', [('name', '=', row[20].value)])

You can also use search_read to get the entire record values.

states = sock.execute(dbname, uid, pwd, '', 'search_read',[('country_id', '=', country_id), ])

This is to search and return only 'internal reference' field of the partner

my_partners = sock.execute(dbname, uid, pwd, 'res.partner', 'search_read', [['id', '=', partner_id]],['ref'])

d) Create record in the Odoo

Create a customer (res.partner) in the Odoo with the create method.

data = {
    'is_company': True,
    'customer': True,
    'supplier': False,
    'type': 'contact',
    'property_account_receivable_id': 47,  
    'property_account_payable_id': 48,   
partner_id = sock.execute(dbname, uid, pwd, 'res.partner', 'create', data)

e) Write or update record in the Odoo

Update the internal reference by the partner id.

sock.execute_kw(dbname, uid, pwd, 'res.partner', 'write',
                                    [partner_id, {'ref': '123'}])

f) unlink() to delete a record

g) Read from the Excel file

You can read from the Excel file, sheet and from the row 0 to the max row. Row[0] is the column A.

workbook = xlrd.open_workbook('migration file name.xls')
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
while curr_row <= num_rows:
	row = worksheet.row(curr_row)

515 views0 comments


bottom of page