Odoo ORM Expert
Overview
This skill teaches you Odoo's Object Relational Mapper (ORM) in depth. It covers reading/writing records, building domain filters, working with relational fields, and avoiding common performance pitfalls like N+1 queries.
When to Use This Skill
- Writing
search(), browse(), create(), write(), or unlink() calls.
- Building complex domain filters for views or server actions.
- Implementing computed, stored, and related fields.
- Debugging slow queries or optimizing bulk operations.
How It Works
- Activate: Mention
@odoo-orm-expert and describe what data operation you need.
- Get Code: Receive correct, idiomatic Odoo ORM code with explanations.
- Optimize: Ask for performance review on existing ORM code.
Examples
Example 1: Search with Domain Filters
# Find all confirmed sale orders for a specific customer, created this year
import datetime
start_of_year = datetime.date.today().replace(month=1, day=1).strftime('%Y-%m-%d')
orders = self.env['sale.order'].search([
('partner_id', '=', partner_id),
('state', '=', 'sale'),
('date_order', '>=', start_of_year),
], order='date_order desc', limit=50)
# Note: pass dates as 'YYYY-MM-DD' strings in domains,
# NOT as fields.Date objects — the ORM serializes them correctly.
Example 2: Computed Field
total_order_count = fields.Integer(
string='Total Orders',
compute='_compute_total_order_count',
store=True
)
@api.depends('sale_order_ids')
def _compute_total_order_count(self):
for record in self:
record.total_order_count = len(record.sale_order_ids)
Example 3: Safe Bulk Write (avoid N+1)
# ✅ GOOD: One query for all records
partners = self.env['res.partner'].search([('country_id', '=', False)])
partners.write({'country_id': self.env.ref('base.us').id})
# ❌ BAD: Triggers a separate query per record
for partner in partners:
partner.country_id = self.env.ref('base.us').id
Best Practices
- ✅ Do: Use
mapped(), filtered(), and sorted() on recordsets instead of Python loops.
- ✅ Do: Use
sudo() sparingly and only when you understand the security implications.
- ✅ Do: Prefer
search_count() over len(search(...)) when you only need a count.
- ✅ Do: Use
with_context(...) to pass context values cleanly rather than modifying self.env.context directly.
- ❌ Don't: Call
search() inside a loop — this is the #1 Odoo performance killer.
- ❌ Don't: Use raw SQL unless absolutely necessary; use ORM for all standard operations.
- ❌ Don't: Pass Python
datetime/date objects directly into domain tuples — always stringify them as 'YYYY-MM-DD'.
Limitations
- Does not cover
cr.execute() raw SQL patterns in depth — use the Odoo performance tuner skill for SQL-level optimization.
- Stored computed fields can cause significant write overhead at scale; this skill does not cover partitioning strategies.
- Does not cover transient models (
models.TransientModel) or wizard patterns.
- ORM behavior can differ slightly between Odoo SaaS and On-Premise due to config overrides.
1---2name: odoo-orm-expert3description: Master Odoo ORM patterns: search, browse, create, write, domain filters, computed fields, and performance-safe query techniques.4---56# Odoo ORM Expert78## Overview910This skill teaches you Odoo's Object Relational Mapper (ORM) in depth. It covers reading/writing records, building domain filters, working with relational fields, and avoiding common performance pitfalls like N+1 queries.1112## When to Use This Skill1314- Writing `search()`, `browse()`, `create()`, `write()`, or `unlink()` calls.15- Building complex domain filters for views or server actions.16- Implementing computed, stored, and related fields.17- Debugging slow queries or optimizing bulk operations.1819## How It Works20211. **Activate**: Mention `@odoo-orm-expert` and describe what data operation you need.222. **Get Code**: Receive correct, idiomatic Odoo ORM code with explanations.233. **Optimize**: Ask for performance review on existing ORM code.2425## Examples2627### Example 1: Search with Domain Filters2829```python30# Find all confirmed sale orders for a specific customer, created this year31import datetime3233start_of_year = datetime.date.today().replace(month=1, day=1).strftime('%Y-%m-%d')3435orders = self.env['sale.order'].search([36 ('partner_id', '=', partner_id),37 ('state', '=', 'sale'),38 ('date_order', '>=', start_of_year),39], order='date_order desc', limit=50)4041# Note: pass dates as 'YYYY-MM-DD' strings in domains,42# NOT as fields.Date objects — the ORM serializes them correctly.43```4445### Example 2: Computed Field4647```python48total_order_count = fields.Integer(49 string='Total Orders',50 compute='_compute_total_order_count',51 store=True52)5354@api.depends('sale_order_ids')55def _compute_total_order_count(self):56 for record in self:57 record.total_order_count = len(record.sale_order_ids)58```5960### Example 3: Safe Bulk Write (avoid N+1)6162```python63# ✅ GOOD: One query for all records64partners = self.env['res.partner'].search([('country_id', '=', False)])65partners.write({'country_id': self.env.ref('base.us').id})6667# ❌ BAD: Triggers a separate query per record68for partner in partners:69 partner.country_id = self.env.ref('base.us').id70```7172## Best Practices7374- ✅ **Do:** Use `mapped()`, `filtered()`, and `sorted()` on recordsets instead of Python loops.75- ✅ **Do:** Use `sudo()` sparingly and only when you understand the security implications.76- ✅ **Do:** Prefer `search_count()` over `len(search(...))` when you only need a count.77- ✅ **Do:** Use `with_context(...)` to pass context values cleanly rather than modifying `self.env.context` directly.78- ❌ **Don't:** Call `search()` inside a loop — this is the #1 Odoo performance killer.79- ❌ **Don't:** Use raw SQL unless absolutely necessary; use ORM for all standard operations.80- ❌ **Don't:** Pass Python `datetime`/`date` objects directly into domain tuples — always stringify them as `'YYYY-MM-DD'`.8182## Limitations8384- Does not cover **`cr.execute()` raw SQL** patterns in depth — use the Odoo performance tuner skill for SQL-level optimization.85- **Stored computed fields** can cause significant write overhead at scale; this skill does not cover partitioning strategies.86- Does not cover **transient models** (`models.TransientModel`) or wizard patterns.87- ORM behavior can differ slightly between Odoo SaaS and On-Premise due to config overrides.