Chapter 14 - Odoo 15 Development Book

XML RPC

It's a spec and a group of implementations that allow software running on disparate operating systems, running in several environments to create procedure calls over the net.

It's a remote procedure call. HTTP is used for the transport and XML for encoding. XML-RPC is intended to be as simple as possible while allowing complex data structures to be transmitted, processed, and returned.

Connection to Odoo:

To access data in Odoo, first, we need to create a connection with odoo. There are two types of xmlrpc endpoints in Odoo. xmlrpc/2/common endpoint didn’t require authentication. This endpoint is used to authenticate the user or fetch the version of odoo. The authenticate method returns the id of the user.

API Keys

From odoo 14.0, we can generate an API key for a user. In order to add a key to the user, Turn on the developer mode. Select Preference from the user My Profile.

odoo-development-book

Select the Account Security Tab, then click on the New API key

odoo-development-book

Confirm the account password and Give a name for the Api key.

odoo-development-book

After that you will get the api key.

Connecting to Odoo using the api key

import xmlrpc.client
url = 'http://localhost:8015' # odoo instance url
database = 'data' # database name
user = 'admin' # username
password = '75d90f11aff5b58768c433cac87ab915bf71116c' api key
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(database, user, password, {})

By using the xmlrpc/2/common we can authenticate the user with database name, user login and the api key as password. The authenticate method returns the user id. Save that user id (Needed for accessing data from odoo).

Calling Methods:

The second endpoint is xmlrpc/2/object which will be used to access data from odoo or create data in odoo. execute_kw method of xmlrpc/2/object will help us to access the data.

model = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

xmlrpc/2/object required authentication so we will be using the user id created while connecting to odoo.

Search/Read records:

Search:

partners_ids = model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]])

The search method returns the id of all the records in the specified model.

output :

[14, 10, 11, 15, 41, 1, 12, 13, 9]

Search with limit:

partners = model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})

Using the limit option we can control how many records we want.

Output:

[14]

Search with Offset:

partners = model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'offset': 1})

Using the offset option we can control from which record we want to search.

Output:

[10, 11, 15, 41, 1, 12, 13, 9]

Search Count:

partners = model.execute_kw(database, uid, password, 'res.partner', 'search_count', [[['is_company', '=', True]]])

Search count returns the number of records that satisfy the condition.

Read:

Partner_id = model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})
partners = model.execute_kw(database, uid, password, 'res.partner', 'read', [partners_id])

Read method returns the values of each field as a key value pair. We need to provide the ids of the record we want to fetch.

Output:

[{'id': 14, 'message_is_follower': False, 'message_follower_ids': [], 'message_partner_ids': [], 'message_ids': [], 'has_message': False, 'message_unread': False, 'message_unread_counter': 0, 'message_needaction': False, 'message_needaction_counter': 0, 'message_has_error': False, 'message_has_error_counter': 0, 'message_attachment_count': 0, 'message_main_attachment_id': False, 'website_message_ids': [], 'message_has_sms_error': False, 'phone_sanitized': '(870)-931-0505', 'phone_sanitized_blacklisted': False, 'phone_blacklisted': False, 'mobile_blacklisted': False, 'phone_mobile_search': False, 'email_normalized': 'azure.interior24@example.com', 'is_blacklisted': False, 'message_bounce': 0, 'activity_ids': [], 'activity_state': False, 'activity_user_id': False, 'activity_type_id': False, 'activity_type_icon': False, 'activity_date_deadline': False, 'my_activity_date_deadline': False, 'activity_summary': False, 'activity_exception_decoration': False, 'activity_exception_icon': False, 'name': 'Azure Interior', 'display_name': 'Azure Interior', 'date': False, 'title': False, 'parent_id': False, 'parent_name': False, 'child_ids': [26, 33, 27], 'ref': False, 'lang': 'en_US', 'active_lang_count': 1, 'tz': False, 'tz_offset': '+0000', 'user_id': False, 'vat': False, 'same_vat_partner_id': False, 'bank_ids': [], 'website': 'http://www.azure-interior.com', 'comment': False, 'category_id': [5], 'credit_limit': 0.0, 'active': True, 'employee': False, 'function': False, 'type': 'contact', 'street': '4557 De Silva St', 'street2': False, 'zip': '94538', 'city': 'Fremont', 'state_id': [13, 'California (US)'], 'country_id': [233, 'United States'], 'country_code': 'US', 'partner_latitude': 0.0, 'partner_longitude': 0.0, 'email': 'azure.Interior24@example.com', 'email_formatted': '"Azure Interior" ', 'phone': '(870)-931-0505', 'mobile': False, 'is_company': True, 'industry_id': False, 'company_type': 'company', 'company_id': False, 'color': 0, 'user_ids': [], 'partner_share': True, 'contact_address': 'Azure Interior\n4557 De Silva St\n\nFremont CA 94538\nUnited States', 'commercial_partner_id': [14, 'Azure Interior'], 'commercial_company_name': 'Azure Interior', 'company_name': False, 'barcode': False, 'self': [14, 'Azure Interior'], '__last_update': '2022-03-24 07:18:33', 'create_uid': [1, 'OdooBot'], 'create_date': '2022-03-24 07:17:52', 'write_uid': [1, 'OdooBot'], 'write_date': '2022-03-24 07:18:33', 'im_status': 'im_partner', 'channel_ids': [], 'signup_token': False, 'signup_type': False, 'signup_expiration': False, 'signup_valid': False, 'signup_url': False, 'property_product_pricelist': [1, 'Public Pricelist (EUR)'], 'partner_gid': 0, 'additional_info': False, 'property_stock_customer': [5, 'Partner Locations/Customers'], 'property_stock_supplier': [4, 'Partner Locations/Vendors'], 'picking_warn': 'no-message', 'picking_warn_msg': False, 'x_encrypted': False}]

Read with fields:

Partner_id = model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})
partners = model.execute_kw(database, uid, password, 'res.partner', 'read', [partners_id], {'fields': ['name', 'email']})

Using the fields option we can specify which fields we want to fetch.

Output:

[{'id': 14, 'name': 'Azure Interior', 'email': 'azure.Interior24@example.com'}]

Search Read:

partners = model.execute_kw(database, uid, password, 'res.partner', 'search_read', [[['is_company', '=', True]]], {'fields': ['name', 'email'], 'limit': 1})

Search read is the combination of both search and read

Output:

[{'id': 14, 'name': 'Azure Interior', 'email': 'azure.Interior24@example.com'}]

Create records:

partner_id = model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])

Create method is used for creating a record in odoo. It returns the id of the created records.

Output:

1

Update records:

partner_id = model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])
partner = model.execute_kw(database, uid, password, 'res.partner', 'write', [[partner_id], {'name': 'Test Partner Updated'}])

The write method is used to update the data in an existing record. It returns True if the record is updated.

Output:

True

Delete Records:

partner_id = model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])
partner_id = model.execute_kw(database, uid, password, 'res.partner', 'unlink', [[partner_id]])

The unlink method is used to delete a record. It returns true if the record is deleted.

Output:

True
whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message