A powerful Python package for interacting with Odoo platforms via JSON-RPC. This library provides a seamless interface for performing CRUD operations, managing sessions, and executing custom methods on your Odoo instance.
- π Secure session management and authentication
- π Complete CRUD operations support
- π Automatic session renewal
- π― Custom method execution
- π‘οΈ Comprehensive error handling
- π Smart record browsing and caching
- π Efficient batch operations
- π Context management
- π Session ID based authentication (New in 0.1.6)
pip install pyodoo_connect --upgrade
- Python 3.6+
- httpx>=0.24.0
- Access to an Odoo instance with JSON-RPC enabled
You can connect to Odoo in two ways:
from pyodoo_connect import connect_odoo
# Connect using credentials
odoo, session_id = connect_odoo(
url="https://your-odoo-instance.com",
db="your_database",
username="your_username",
password="your_password"
)
from pyodoo_connect import connect_odoo
# Connect using existing session ID
odoo, session_id = connect_odoo(
url="https://your-odoo-instance.com",
session_id="your_session_id"
)
# Create a single partner
partner_id = odoo.env('res.partner').create({
'name': 'John Doe',
'email': '[email protected]',
'phone': '+1234567890',
'is_company': True
})
# Create with related records
from pyodoo_connect import Command
sales_order = odoo.env('sale.order').create({
'partner_id': partner_id,
'order_line': [Command.create({
'product_id': 1,
'product_uom_qty': 2,
'price_unit': 100
})]
})
# Browse a single record
partner = odoo.env('res.partner').browse(partner_id)
print(f"Partner Name: {partner.name}")
print(f"Email: {partner.email}")
# Search and read multiple records
partners = odoo.env('res.partner').search_read(
domain=[('is_company', '=', True)],
fields=['name', 'email', 'phone'],
limit=5
)
for partner in partners:
print(f"Company: {partner['name']}")
# Update using write method
odoo.env('res.partner').write([partner_id], {
'name': 'John Smith',
'email': '[email protected]'
})
# Update using record attribute
partner = odoo.env('res.partner').browse(partner_id)
partner.phone = '+9876543210'
# Delete a single record
odoo.env('res.partner').unlink([partner_id])
# Delete multiple records
partner_ids = odoo.env('res.partner').search([
('name', 'like', 'Test%')
])
odoo.env('res.partner').unlink(partner_ids)
Using Command class for managing relations:
from pyodoo_connect import Command
# Link existing records
partner.category_id = [Command.set([1, 2, 3])] # Set specific tags
# Create and link new record
partner.child_ids = [
Command.create({
'name': 'Contact Person',
'email': '[email protected]'
})
]
# Update linked record
partner.child_ids = [
Command.update(child_id, {
'name': 'New Name'
})
]
# Unlink records
partner.category_id = [Command.unlink(tag_id)]
# Clear all relations
partner.category_id = [Command.clear()]
# Basic search
customer_ids = odoo.env('res.partner').search([
('customer_rank', '>', 0),
('is_company', '=', True)
])
# Search with additional parameters
products = odoo.env('product.product').search_read(
domain=[('type', '=', 'product')],
fields=['name', 'list_price', 'qty_available'],
offset=0,
limit=10,
order='name ASC'
)
# Set context for specific operations
order = odoo.env('sale.order').browse(order_id)
order.with_context(force_company=2).action_confirm()
# Multiple context values
result = order.with_context({
'lang': 'es_ES',
'tz': 'Europe/Madrid',
'force_company': 2
}).action_invoice_create()
# Execute custom methods on records
sale_order = odoo.env('sale.order').browse(order_id)
sale_order.action_confirm() # Confirm sale order
# Execute with parameters
invoice = sale_order.with_context(default_type='out_invoice')._create_invoices()
The library provides specific exceptions for different error scenarios:
from pyodoo_connect import (
OdooException,
OdooConnectionError,
OdooAuthenticationError,
OdooRequestError,
OdooValidationError
)
try:
odoo.env('res.partner').create({
'name': 'Test',
'email': 'invalid_email' # This will raise a validation error
})
except OdooValidationError as e:
print(f"Validation Error: {str(e)}")
except OdooConnectionError as e:
print(f"Connection Error: {str(e)}")
except OdooException as e:
print(f"General Odoo Error: {str(e)}")
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
- Name: Fasil
- Email: [email protected]
- WhatsApp: Contact