Odoo 16 Development Book

XML RPC

It is a specification and a collection of implementations that allow procedure calls to be made over the internet by software running in different environments and on different operating systems.

It's a remote operation that's being asked for. XML is utilized for encoding, while HTTP is used for transport. Though it facilitates the transmission, processing, and return of complex data structures, XML-RPC is designed to be as simple as is practicable.

Connection to Odoo

In order to access data inside Odoo, we must first connect to it. The xmlrpc/2/common endpoint, one of two types available in Odoo, does not require authentication.

Use this endpoint to get the version of Odoo or to verify the user's identity. The authenticating process returns the user's ID.

API Key

We may generate an API key for a user using Odoo 16.0. For adding a key to the user, enable developer mode. Select the Preferences menu from the user’s My Profile, as depicted below.

odoo Development

Select the Account Security Tab, and click on the New API key.

odoo Development

Give the API key a name and confirm the account's password.

odoo Development

You will then receive the API key.

You can utilize the API key to connect to Odoo.

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

With the database name, user login, and API key as the password, we can authenticate the user using the xmlrpc/2/common. User ID is a result of the authenticate function. Jot down that user ID (Needed for accessing data from Odoo).

Calling Methods

The second endpoint, xmlrpc/2/object, is used to access data stored in Odoo or to create new data within Odoo. We can get the data with the use of the xmlrpc/2/execute kw object's method.

data_model = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(data_url))

The user ID created when connecting to Odoo will be used since xmlrpc/2/object requires authentication.

Read/Search Records:

Search:

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

All record IDs in the chosen model are returned by the search function.

Output

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

Limit your search to:

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

We are able to decide how many records we want by using the limit option.

Output:

[14]

Search with Offset:

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

Using the offset argument, we can select the record from which to start our search.

Output:

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

Search Count:

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

Search count returns the number of records that meet the requirement.

Read:

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

Each field's values are returned by the read method as a key-value pair. The IDs of the records we wish to get must be provided.

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 = data_model.execute_kw(database, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]], {'limit': 1})
partners = data_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 = data_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 = data_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 = data_model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])
partner = data_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 = data_model.execute_kw(database, uid, password, 'res.partner', 'create', [{'name': 'Test Partner', 'email': 'test@test.com'}])
partner_id = data_model.execute_kw(database, uid, password, 'res.partner', 'unlink', [[partner_id]])

A record is removed using the unlink method. If the record is erased, it returns true.

Output:

True
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