
Learn how to use Odoo's External API to search and read data directly from the Odoo database using XML-RPC or JSON-RPC. Efficiently query and retrieve records in Odoo.
Odoo External API stands as powerful characteristic. It allows developers to have exchanges with Odoo from other apps. They can access and adjust data. They can even do intricate operations. All without logging directly into system. The most used feature of the API is to seek and read data from Odoo database. This task is achievable by using XML-RPC and JSON-RPC protocols. This blog aims to explore these methods. It will focus on how to effectively query records.
Odoo’s External API offers third-party applications a way to communicate with Odoo’s backend. It leverages its database models. XML-RPC or JSON-RPC are typically used for this communication. The API itself lets you interact with a model. You can create read, update, or delete records.
To interact with Odoo database via API we require certain things. Namely we need:
The URL for Odoo instance. The name of the database. User name and the password of an authorized Odoo user. These credentials help us establish connection. We connect to Odoo’s backend. Then we can start querying data.
Here’s sample setup. We connect using XML-RPC:
import xmlrpc.client
url = 'http://your-odoo-url.com'
db = 'your-database'
username = 'your-username'
password = 'your-password'
# Authenticate the user
common = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/common')
uid = common.authenticate(db, username, password, {})
# Connect to the object models
models = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/object')
With connection set, we can start searching and reading data.
Method search() in Odoo’s API aids in filtering records. This filtering is based on specific criteria. Method returns record IDs. These IDs match conditions given in domain filter. Domains can combine diverse search criteria. They can also use logical operators. These include & (AND), | (OR) and ! (NOT). These operators are for the purpose of filtering.
To search for records we examine the res.partner model. We look at specific criteria:
partner_ids = models.execute_kw(db, uid, password, 'res.partner', 'search', [[['is_company', '=', True]]])
For res.partner records where partner is company we retrieve partner IDs.
After we obtain record IDs, we gather specific information. We make use of the read() method. This method permits the definition of which fields to obtain. Such a provision makes it efficient. It controls the size of data.
To read fields like name and email for each partner:
partners_data = models.execute_kw(db, uid, password, 'res.partner', 'read', [partner_ids], {'fields': ['name', 'email']})
A list of dictionaries is output. Each dictionary contains fields and values. These values are for every res.partner record. These records are gathered through search().
The API of Odoo has a search_read() method. This method combines search and read operations. The combination is for a solitary request. This strategy reduces API calls. It's particularly useful for managing a significant number of records.
The task is to fetch partner records. We need to use both search and read in one command.
partners = models.execute_kw(db, uid, password, 'res.partner', 'search_read', [[['is_company', '=', True]]], {'fields': ['name', 'email']})
This command will return partner records. It will return only companies. Additionally it will include name and email fields in each record.
Search_read() supports not only search and read. It also supports limiting results and ordering them in a particular way. To limit the results and order them, you can use limit and order options.
partners_limited = models.execute_kw(db, uid, password, 'res.partner', 'search_read', [[['is_company', '=', True]]], {'fields': ['name', 'email'], 'limit': 5, 'order': 'name ASC'})
Fetches only five partner records. It orders them alphabetically by name.
Odoo's External API is a powerful resource for querying. One can retrieve data from its database with robust methods. The combination of search read and search_read methods ensures records are efficiently accessed and controlled. With these Odoo's functionality can be extended by developers. They can connect with third-party applications. They can also offer smooth data integration solutions.
Odoo's search and read capabilities can be leveraged. Through it, a more interactive application experience can be created. Additionally, a data-driven experience is achievable. This allows for harnessing full potential of your Odoo instance.
Your email address will not be published. Required fields are marked *