Searchable Non-Stored Compute Fields in Odoo 18.

Odoo 18 provides powerful tools to define computed fields, which can be used to display dynamic values. However, one common challenge developers face is making these computed fields searchable when they are not stored in the database. In this blog, we will explore how to create a searchable non-stored compute field in Odoo 18, along with best practices.

What is a Non-Stored Compute Field?

A computed field in Odoo is a field whose value is dynamically calculated using a function rather than being directly stored in the database. By default, computed fields are not stored unless explicitly specified with store=True. This means they do not take up space in the database but are recomputed each time they are accessed.

Why Make a Compute Field Searchable?

In Odoo, search functionality relies on database queries. Since non-stored computed fields do not exist in the database, they are not automatically searchable. However, we can define a search function to make them searchable using related database fields.

How to Create a Searchable Non-Stored Compute Field in Odoo 18:

Follow these steps to create a computed field that is both non-stored and searchable:

Step 1: Define the Compute Field:

from odoo import models, fields, api

class ProductTemplate(models.Model):
_inherit = ‘product.template’

display_name_upper = fields.Char(
    string="Uppercase Name",
    compute="_compute_display_name_upper",
    search="_search_display_name_upper"
)

@api.depends('name')
def _compute_display_name_upper(self):
    for record in self:
        record.display_name_upper = record.name.upper() if record.name else ""
In this example, display_name_upper dynamically converts the name field to uppercase. It is not stored but is computed on the fly
Step 2: Implement the Search Function:
def _search_display_name_upper(self, operator, value):
        return [('name', operator    , value)]

This function ensures that when users search for display_name_upper, Odoo redirects the search query to the name field in the database.

Best Practices for Searchable Non-Stored Compute Fields:
  • Use Efficient Search Functions – Ensure the search function maps to a real database field to avoid performance issues.
  • Keep Computations Lightweight – Since these fields are computed on the fly, avoid complex operations that slow down response time.
  • Use Indexes on Related Fields – If your search function relies on a related field, ensure that field is indexed for faster searches.
Conclusion:

By following the approach outlined above, you can successfully create searchable non-stored compute fields in Odoo 18. This allows you to keep your database optimized while ensuring powerful search functionality. Implement these techniques in your Odoo modules and enhance your application’s efficiency!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *