"Boost Odoo Performance with @lazy_property: A Smart Cache"

View profile for Jayank Aghara 🇮🇳

Sr. Software Engineer (Odoo) | Python Developer | Freelancer

🚀 Boost Your Odoo Performance with @lazy_property When working with Odoo, every millisecond counts — especially on heavy models like res.partner or sale.order. That’s where the @lazy_property decorator comes in. 🔹 What is @lazy_property? @lazy_property is a decorator in Odoo (odoo.tools) that allows you to compute a value once per record and reuse it without recalculating during the same request. Think of it as a smart cache for model properties. 🔹 Why should we use it? - Prevents repeated database queries within a single request. - Optimizes performance for expensive calculations. - Keeps the code cleaner (no manual caching logic needed). - Directly improves runtime efficiency 🔹 Where can we use it? Use @lazy_property when: - The value doesn’t change during the request lifecycle. - It requires heavy DB operations or complex calculations. - Example: counting related records, aggregated values, pre-processed settings. 🔹 How can we use it? Please look at the attached image for code snippet: ✅ The first time you access partner.sale_order_count, it runs the query. ✅ Next calls in the same request will use the cached result — no extra queries! 🔹 How does it reduce runtime & improve Odoo experience? - Without @lazy_property: each call executes a new DB query. - With @lazy_property: only one query per record per request. - Result: faster views, optimized API responses, and smoother user experience. 💡 Pro Tip: Don’t overuse it. If the value can change within the same request, avoid caching with @lazy_property. ❓ Open Question Have you already used @lazy_property in your Odoo projects? 👉 If yes, what’s your favorite use case? If not, where do you think it could save you the most performance? #Odoo #OdooDevelopment #OdooTips #EnterpriseSoftware #ERP #BackendDevelopment #SoftwareEngineering #DataProcessing #PerformanceOptimization #OpenSource

  • text
William André

Software Developer at Odoo

1mo

From the example given it looks line you didn't understand the way it works and its purpose. It is not cached per request but for the whipe life of the server. It has even been replaced by `functools.cached_property` in Odoo 19.

Ibrahim Boudmir

Tech Lead Odoo, certifié Odoo V18

2mo

I didn’t know Odoo provided a decorator for lazy_property function (from odoo.tools.func import lazy_property). I believe they do the same thing. Definitely adding this one to my toolbox. Thanks!

Babar Khan

Python | Odoo Developer | HTML | CSS | Bootstrap | GIT | GitLab | odoo.sh

2mo
Yahia Shokry

Python Developer | Odoo Backend Developer

1mo

I can put it directly in a variable and use it , what is the difference?

Nikhil Nakrani 🇮🇳

Senior Software Engineer | OdooV18 Certified | Python 🐍 | JavaScript | GCP | Linux | Top 150 Odoo Forum Contributors

2mo

Thanks for sharing, Jayank

Anjali Singh

B.Tech CSE’24 Odoo Python developer || JAVA || DSA || PYTHON || SQL

1mo

Thanks for sharing

See more comments

To view or add a comment, sign in

Explore content categories