A Python package to interact with Odoo via JSON-RPC.
Project description
PyOdoo Connector
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.
Features
- 🔐 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
Installation
pip install pyodoo_connect
Requirements
- Python 3.6+
- httpx>=0.24.0
- Access to an Odoo instance with JSON-RPC enabled
Quick Start
Basic Connection
from pyodoo_connect import connect_odoo
# Connect to Odoo
odoo, session_id = connect_odoo(
url="https://your-odoo-instance.com",
db="your_database",
username="your_username",
password="your_password"
)
# Check connection
if odoo:
print("Successfully connected!")
print(f"Session ID: {session_id}")
Usage Examples
1. Record Operations
Create Records
# Create a single partner
partner_id = odoo.env('res.partner').create({
'name': 'John Doe',
'email': 'john@example.com',
'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
})]
})
Read Records
# 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 Records
# Update using write method
odoo.env('res.partner').write([partner_id], {
'name': 'John Smith',
'email': 'john.smith@example.com'
})
# Update using record attribute
partner = odoo.env('res.partner').browse(partner_id)
partner.phone = '+9876543210'
Delete Records
# 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)
2. Relational Fields Operations
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': 'contact@example.com'
})
]
# 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()]
3. Search Operations
# 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'
)
4. Context Management
# 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()
5. Custom Method Execution
# 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()
6. Error Handling
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)}")
Contributing
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.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
- Name: Fasil
- Email: fasilwdr@hotmail.com
- WhatsApp: Contact
- Facebook: Profile
- Instagram: Profile
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyodoo_connect-0.1.5.tar.gz.
File metadata
- Download URL: pyodoo_connect-0.1.5.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21eb769880e84682a6ef11ef76cfedebaf3cfef46f497d1ce91fc01fad38f1d8
|
|
| MD5 |
7f789da6be212c99b71321652ca759c5
|
|
| BLAKE2b-256 |
2e205fda96c4ea559bf081b588a7a12c56a24612e5f24821a1331566d1e73be9
|
File details
Details for the file pyodoo_connect-0.1.5-py3-none-any.whl.
File metadata
- Download URL: pyodoo_connect-0.1.5-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
894610be534931e9248ec50092401897ede270a0f5061f4b23b01bc7e839ef6e
|
|
| MD5 |
b29fd66f8b25e0fe898b3949ebaec814
|
|
| BLAKE2b-256 |
8b1ce0f6691436fd564f0cd804e79e4b147ac01778623d2e602804b6a0431741
|