Odoo RPC Client

A high-level client for interacting with Odoo’s XML-RPC/JSON-RPC API.

Configuration

The OdooRPCClient can be configured using environment variables for convenience. All parameters are optional and can be overridden when initializing the client.

Variable

Type

Default

Description

ODOO_HOST

string

None

Odoo server hostname or IP address

ODOO_PORT

int

8069

Odoo server port number

ODOO_PROTOCOL

string

jsonrpc

Protocol to use (jsonrpc or xmlrpc)

ODOO_DB

string

None

Database name to connect to

ODOO_USER

string

None

Username for authentication

ODOO_PASSWORD

string

None

Password for authentication

Example .env file:

ODOO_HOST=localhost
ODOO_PORT=8069
ODOO_PROTOCOL=jsonrpc
ODOO_DB=my_database
ODOO_USER=admin
ODOO_PASSWORD=admin_password

When using environment variables, you can initialize the client without any parameters:

from prs_commons.odoo.rpc_client import OdooRPCClient

# Uses environment variables for configuration
client = OdooRPCClient()

# You can still override specific parameters
client = OdooRPCClient(
    host='custom-host',  # Overrides ODOO_HOST
    database='other_db'  # Overrides ODOO_DB
)

API Reference

class OdooRPCClient(host=None, database=None, username=None, password=None, protocol='jsonrpc', port=8069, **kwargs)

Initialize the Odoo RPC client.

Parameters:
  • host (str) – Odoo server hostname or IP (default: from environment)

  • database (str) – Database name (default: from environment)

  • username (str) – Login username (default: from environment)

  • password (str) – Login password (default: from environment)

  • protocol (str) – Protocol to use (‘jsonrpc’ or ‘xmlrpc’)

  • port (int) – Port number (default: 8069)

  • kwargs – Additional connection parameters

Note

This is a singleton class. Multiple instantiations will return the same instance.

ensure_connection()

Ensure connection to the Odoo server is established.

Raises:
search_read(model, domain=None, fields=None, offset=0, limit=None, order=None, **kwargs)

Search and read records matching the criteria.

Parameters:
  • model (str) – Name of the Odoo model (e.g., ‘res.partner’)

  • domain (list) – Search domain (list of tuples)

  • fields (list) – List of fields to return

  • offset (int) – Number of records to skip

  • limit (int) – Maximum number of records to return

  • order (str) – Sort order (e.g., ‘name asc, id desc’)

Returns:

List of dictionaries containing record data

create_record(model, values, **kwargs)

Create a new record in the specified model.

Parameters:
  • model (str) – Name of the Odoo model

  • values (dict) – Field values for the new record

Returns:

ID of the created record

write_record(model, ids, values, **kwargs)

Update existing record(s).

Parameters:
  • model (str) – Name of the Odoo model

  • ids (list) – List of record IDs to update

  • values (dict) – Field values to update

Returns:

True if successful

execute_method(model, method, *args, **kwargs)

Execute a method on the Odoo model.

Parameters:
  • model (str) – Name of the Odoo model

  • method (str) – Method name to call

  • args – Positional arguments for the method

  • kwargs – Keyword arguments for the method

Returns:

Result of the method call

Example Usage

from prs_commons.odoo.rpc_client import OdooRPCClient

# Initialize the client (singleton pattern)
client = OdooRPCClient()

# Ensure connection
client.ensure_connection()

# Search for partner records
domain = [('is_company', '=', True)]
fields = ['id', 'name', 'email']
partners = client.search_read('res.partner', domain, fields=fields)

# Create a new partner
new_id = client.create_record('res.partner', {
    'name': 'Acme Inc.',
    'is_company': True,
    'email': 'info@acme.com'
})

# Update the partner
client.write_record('res.partner', [new_id], {
    'email': 'contact@acme.com'
})

# Execute a method
result = client.execute_method('res.partner', 'search_read', domain, fields=fields)

# Close the connection
client.close_connection()

Error Handling

The client raises appropriate exceptions for different error conditions:

See Also