Hey there, fellow developer! Ready to dive into the world of Odoo ERP API integration? Great! We'll be using the awesome erppeek
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
erppeek
installed (pip install erppeek
)First things first, let's establish a connection:
import erppeek client = erppeek.Client( 'http://localhost:8069', 'your_database', 'your_username', 'your_password' )
If everything's set up correctly, you're now connected! If not, don't panic – double-check your credentials and make sure your Odoo instance is running.
Now for the fun part – let's play with some data!
new_partner = client.model('res.partner').create({ 'name': 'John Doe', 'email': '[email protected]' })
partner = client.model('res.partner').get(new_partner) print(partner.name) # Output: John Doe
partner.write({'phone': '123-456-7890'})
partner.unlink()
Let's get fancy with our data retrieval:
partners = client.model('res.partner').search_read( [('customer_rank', '>', 0)], ['name', 'email'], order='create_date desc', limit=5 )
This fetches the 5 most recently created customers, returning only their names and emails.
Odoo's relational fields are a breeze with erppeek
:
# Many2one company = client.model('res.company').get(1) partner.company_id = company # One2many for child in partner.child_ids: print(child.name) # Many2many partner.category_id = [1, 2, 3] # Set tags by ID
Need to call a specific Odoo method? No problem:
invoice = client.model('account.move').get(1) invoice.action_post() # Confirm the invoice
Always be prepared for the unexpected:
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) try: result = client.execute('res.partner', 'check_access_rights', 'read') except Exception as e: logger.error(f"An error occurred: {str(e)}")
search_read
instead of separate search
and read
calls for better performance.And there you have it! You're now equipped to build powerful Odoo integrations using Python and erppeek
. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do.
For more in-depth examples and a complete code repository, check out my GitHub repo [link to your repo]. Happy coding, and may your integrations be ever smooth and bug-free!