Enable Dark Mode!
an-overview-of-odoo-rpc-client.jpg
By: Syamili K

An Overview of Odoo RPC Client

Technical

The Odoo RPC (Remote Procedure Call) client is the one of the key components in enabling smooth communication between applications and an Odoo instance via RPC(XML-RPC, JSON-RPC). This powerful tool performs a wide range of activities, such as establishing a connection to the server, gathering connection data, database management, and using ORM (Object-Relational Mapping) methods.

Installation

The odoo_rpc_client package is used to connect Odoo instances.

odoo_rpc_client is present on PyPI, so it could be installed via PIP.

pip install odoo_rpc_client

Connect to server

For connecting an Odoo instance, you first need to set up a connection to the server. With the help of those connections, you can access the database on that server.

Two different methods can be used to set up the connection.

Method 1

from odoo_rpc_client import Client
database_instance = Client(host='localhost', port=8018)
database_logined=database_instance.login(dbname='db_1', user='1', password='1', protocol='xml-rpc')

Method 2

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1', protocol='xml-rpc')

Connection Info Collecting

The name of the database currently connected.

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.dbname

The name of the User login used to access the DB

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.username

The instance of the User login used to access the DB

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.user

The database stored id of the User login used to access the DB

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.uid

The context information of the User login used to access the DB

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.user_context

Server host information

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.host

Server port information

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.port

Connected server version information

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.server_version

Connected database full version information

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.database_version_full

Connected database version information

database.database_version
Connection protocol information
from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.protocol

Reconnect DB and clear caches information

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.reconnect()

Clear caches information

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.clean_caches()

Connected url information

database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
database.get_url()

Database Management

Database management in Odoo using the RPC client involves performing various operations such as creating, restoring, and dropping databases. The Odoo RPC client allows you to interact with the Odoo server to execute these database management tasks programmatically.

Creating new Database

from odoo_rpc_client import service
service.db.DBService.create_db('anoop', 'db_1111', True, 'en_US', 'anoop')

Drop existing Database

from odoo_rpc_client import service
service.db.DBService.drop_db(password='master_password', db='DB_name')

Restoring Database

from odoo_rpc_client import service
service.db.DBService.restore_db(password='master_password', dbname='db_name', data='base64 encoded string')from odoo_rpc_client import service
service.db.DBService.db_exist(db='db_name')

ORM Methods

When working with Odoo's RPC client to interact with the Odoo server, you often perform operations on Odoo models using ORM (Object-Relational Mapping) methods. The ORM methods allow you to interact with the database by creating, reading, updating, searching and browsing records. Commonly two methods are used, and here are some commonly used ORM methods in Odoo:

Method 1

Search 

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
# Search without condition
sale_orders = database['sale.order'].search([])
print(sale_orders)
# Search with condition
sale_orders = database['sale.order'].search([('state', '=', 'draft')])
print(sale_orders)

Browse

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
#Browse with single id
sale_orders = database['sale.order'].browse(10)
print(sale_orders)
#Browse with multiple id
sale_orders = database['sale.order'].browse([10, 12])
print(sale_orders)
#  Browse with group by condition
sale_orders = database['sale.order'].browse([10, 12]).group_by('state')
print(sale_orders)
#  Browse with group by lambda condition
sale_orders = database['sale.order'].frombrowse([10, 12]).group_by(lambda so: so.name[-1])
print(sale_orders)
#  Browse with sorting condition
sale_orders = database['sale.order'].browse([10, 12]).sort(lambda so: so.id, reverse=True)
print(sale_orders)
#  Browse with filter condition
sale_orders = database['sale.order'].browse([10, 12]).filter(lambda so: so.id == 10)
print(sale_orders)

Search Count

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
# Search count without condition
sale_orders = database['sale.order'].search_count()
print(sale_orders)
# Search count with condition
sale_orders = database['sale.order'].search_count([('state', '=', 'draft')])
print(sale_orders)

Search Record

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
# Search Record without condition
sale_orders = database['sale.order'].search_records([])
print(sale_orders)
# Search Record with condition
sale_orders = database['sale.order'].search_records([('state', '=', 'draft')])
print(sale_orders)
# Search Record with group by condition
sale_orders = database['sale.order'].search_records([]).group_by('state')
print(sale_orders)
# Search Record with group by lambda condition
sale_orders = database['sale.order'].search_records([]).group_by(lambda so: so.name[-1])
print(sale_orders)
# Search Record with sort condition
sale_orders = database['sale.order'].search_records([]).sort(lambda so: so.id, reverse=True)
print(sale_orders)
# Search Record with filter condition
sale_orders = database['sale.order'].search_records([]).filter(lambda so: so.id == 10)
print(sale_orders)

Read Record

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
# Read Record condition
sale_orders = database['sale.order'].read_records([10, 12])
print(sale_orders)
# Read Record with group by condition
sale_orders = database['sale.order'].read_records([10, 12]).group_by('state')
print(sale_orders)
# Read Record with group by lambda condition
sale_orders = database['sale.order'].read_records([10, 12]).group_by(lambda so: so.name[-1])
print(sale_orders)
# Read Record with sort condition
sale_orders = database['sale.order'].read_records([10, 12]).sort(lambda so: so.id, reverse=True)
print(sale_orders)
# Read Record with filter condition
sale_orders = database['sale.order'].read_records([10, 12]).filter(lambda so: so.id == 10)
print(sale_orders)

Read

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
# Read condition
sale_orders = database['sale.order'].read([10], ['id', 'name'])
print(sale_orders)

Create

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
partner_detail = database['res.partner'].create({'name': 'Odoo Rpc Client'})
print(partner_detail)

Update

from odoo_rpc_client import Client
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
partner_detail = database['res.partner'].write([72], {'name': 'Odoo Rpc Client Edited'})
print(partner_detail)

Method 2

Search 

from odoo_rpc_client import Client, orm
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
sale_orders_obj = orm.Object(database, 'sale.order')
# Search without condition
sale_orders = sale_orders_obj.search([])
print(sale_orders)
# Search with condition
sale_orders = sale_orders_obj.search([('state', '=', 'draft')])
print(sale_orders)

Read

from odoo_rpc_client import Client, orm
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
sale_orders_obj = orm.Object(database, 'sale.order')
sale_orders = sale_orders_obj.read([10], ['id', 'name'])
print(sale_orders)

Create

from odoo_rpc_client import Client, orm
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
sale_orders_obj = orm.Object(database, 'sale.order')
new_order_id = sale_orders_obj.create({
   'partner_id': 1,
   'order_line': [(0, 0, {
           'product_id': 1,   
           'product_uom_qty': 2,           
           'price_unit': 100})]
})
print(new_order_id)

Write

from odoo_rpc_client import Client, orm
database = Client(host='localhost', dbname='db_1', port=8018, user='1', pwd='1')
sale_orders_obj = orm.Object(database, 'sale.order')
partner_detail = sale_orders_obj.write([1], {
   'name': 'Odoo Rpc Client'})
print(partner_detail)

In conclusion, using the Odoo platform finds the Odoo RPC client to be an extremely useful and adaptable tool. By providing a stable database management system, a smooth server connection, effective connection information gathering, and support for ORM techniques.

This code provides a basic illustration of how you can use Python's xmlrpc.client library to interact with the Odoo RPC client for various functionalities. Replace the values with your actual Odoo server details, database information, and credentials into your applications as required.



If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



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