How to use search_fetch() for better Odoo ORM performance

View profile for Aravind S

ODOO IMPLEMENTER | PROJECT LEAD | SENIOR ODOO DEVELOPER | PYTHON | JS | CERTIFIED FUNCTIONAL CONSULTANT

🔎 Deep Dive: search_fetch() in the Odoo ORM ⚡ As Odoo developers we often use .search() plus .read() or rely on fields loading on demand. When performance is critical, though, search_fetch() is a cleaner option — it reduces round-trips and loads exactly what you need. 🧠 What search_fetch() does Model.search_fetch(domain, field_names[, offset=0][, limit=None][, order=None]) • Runs a domain search • Retrieves the specified fields in a single step • Prevents additional SQL caused by lazy field access • Keeps results cached for better performance Think of it as a faster, single-query alternative to .search() followed by .read(). 💡 When it’s the right tool Reach for search_fetch() when: • ⚡ You need lower latency — e.g., API responses, dashboards, or server-side routines • 📉 You want to cut down on SQL queries • 📋 You only need a subset of fields (read-only) • 🛑 You don’t plan to alter the returned records 🧪 Example products = self.env['product.template'].search_fetch( domain=[('sale_ok', '=', True)], field_names=['name', 'list_price', 'currency_id'], limit=50, order='name ASC' ) This pulls 50 saleable products and preloads the requested fields in one efficient call. ⚠️ Quick caveats • Returns a standard recordset. • The listed fields are fully loaded into memory (no later lazy fetches). • Will raise AccessError if the user lacks read permission for the model/fields. 🛠️ Good scenarios to adopt it • REST or JSON endpoints • Reporting and analytics code • Custom dashboard backends • Any flow that’s read-heavy and sensitive to DB round-trips 💬 Have you tried search_fetch() in your Odoo apps? Drop your tips or experiences — let’s swap notes and optimize together!

  • graphical user interface, website

To view or add a comment, sign in

Explore content categories