Pull External Data into Odoo
Many businesses run legacy systems or third-party applications that store critical data in separate databases. Integrating this data into Odoo is often necessary for reporting, invoicing, inventory management, or customer engagement.
This guide walks you through a practical, developer-friendly approach to syncing external databases with Odoo using Python.
We’ll explore how to:
- Connect Python to external databases (MySQL, PostgreSQL)
- Fetch and process external data
- Sync it with Odoo models using the ORM
- Automate the process via Scheduled Actions
- Secure and optimise the workflow
Why Sync External Databases with Odoo?
- Centralise business data scattered across systems
- Avoid manual data entry and reduce errors
- Integrate with legacy ERP, CRM, and WMS systems
- Automate regular data imports
- Enable consolidated reporting
Common Use Cases
- Customer or supplier info from a CRM
- Inventory data from a warehouse management system
- Sales records from a legacy POS system
Technical Architecture Overview
[External Database] ←→ [Python script (inside Odoo custom model)] ←→ [Odoo ORM] ←→ [Odoo PostgreSQL Database]
Workflow
- Use Python libraries (mysql-connector-python for MySQL, psycopg2 for PostgreSQL)
- Fetch records from the external DB
- Map fields to Odoo models
- Use the Odoo ORM for create/update operations
- Schedule the sync via Odoo’s Scheduled Actions
Implementation: Full Code Example
Custom Field for Mapping (to avoid duplicates)
Add a technical field like legacy_id to track external records:
Connect to an External MySQL Database
For PostgreSQL
Fetch Records (MySQL example)
Sync Data into Odoo
Automating the Process
Go to Settings → Technical → Automation → Scheduled Actions.
Create a new action
- Model: res.partner
- Python Code:
Security & Best Practices
Never hardcode DB credentials
Use ir.config_parameter to store database credentials securely.
self.env['ir.config_parameter'].sudo().get_param('legacy_db_password')
Log all operations
Use Odoo’s _logger to track every sync run.
Use transactions or batch commits
For large data sets, use Odoo’s env.cr.commit() after every batch to avoid transaction timeouts.
Validate external data before import
Check for missing or malformed fields before writing to Odoo.
Make the process idempotent
Use unique external IDs (legacy_id) to avoid duplicate records.
Possible Enhancements
- Two-way sync (writeback changes from Odoo to external DB)
- Sync attachments or images (if URLs/paths are stored externally)
- Sync other models (products, orders, invoices)
- Notify users via email after each sync
- Sync via Odoo scheduled cron jobs
- Use Odoo external API or OdooRPC instead of direct DB connections for loosely coupled systems
- Migrate to a message queue (RabbitMQ, Redis) for real-time updates
The Takeaway
Connecting Odoo with external databases via Python scripts is a powerful technique for modernising legacy systems and consolidating business data. With careful planning and secure coding practices, you can automate regular sync jobs and free up hours of manual work while reducing errors.