The Odoo Client Library is a Python 3 library designed to facilitate communication with an Odoo Server through its web services in a user-friendly manner. It was developed for users who prefer not to write low-level XML-RPC calls manually. The library supports both XML-RPC and JSON-RPC protocols and offers convenient syntax enhancements to simplify interactions. In this blog, we’ll walk through how to connect to an Odoo instance using odoo-client-lib and make the most of its features.
Installation
First we need to install the odoo-client-lib using the below command
pip install odoo-client-lib
Connecting odoo server
Now we can connect an odoo server using odoo-client-lib using the following code.
import odoolib
class OdooAPI:
def __init__(self, odoo_connection):
self.odoo_connection = odoo_connection
def get_user(self):
user_model = self.odoo_connection.get_model('res.users')
ids = user_model.search([('login','=','a')]) # This returns a list of IDs
records = user_model.read(ids, ['name']) # Read the 'balance' field
users = [rec['name'] for rec in records]
return users
odoo_connection = odoolib.get_connection(hostname="localhost",
database="rest",
login="a",
password="a",
protocol="jsonrpc",
port=8017)
##########################################
odoo_api = OdooAPI(odoo_connection)
user = odoo_api.get_user()
print("user",user)
In the previous script, the get_connection() function sets up a Connection object that handles authenticated communication with an Odoo server. While it defaults to using XML-RPC, you can configure it to use JSON-RPC instead. It's also possible to specify a different port.
connection = odoolib.get_connection(hostname="localhost", protocol="jsonrpc", port=8017, ...)
The get_model() method from the Connection object returns a Model instance, which acts as a reference to a model on the Odoo server (often referred to as osv in addon development). These Model instances function as dynamic proxies, allowing you to invoke remote methods in an intuitive manner. In the previous example, we illustrated how to use the search() and read() methods. The following use case mirrors the way models are accessed and used when writing code for Odoo addons running directly on the server.
user_osv = self.pool.get('res.users')
ids = user_osv.search(cr, uid, [("login", "=", "a")])
user_info = user_osv.read(cr, uid, ids[0], ["name"])
Using Model objects provides some syntactic convenience over traditional addon development:
* There’s no need to explicitly pass cr and uid to each method call.
* The read() method returns records in the same order as the list of IDs provided.
* Additionally, Model objects support a search_read() method, which performs a search and read in a single call. For example:
user = user_model.search_read([('login', '=',’a’)], ["name"])[0]
When working with the Odoo Client Library, keep the following points in mind:
* Since each remote procedure call runs in its own transaction, executing multiple dependent operations separately can be risky. For such scenarios, it's better to implement the logic within an Odoo addon. This ensures that the operations run on the server within a single transaction.
* The browse() method is not supported. In standard Odoo server-side code, browse() provides a proxy object that fetches data lazily from the database. This behavior isn’t available in the client library.
So, this is how we connect to the Odoo server using odoo-client-lib.
To read more about How To Setup Suricata in Odoo Server, refer to our blog How To Setup Suricata in Odoo Server.