Searchable Non-Stored Compute Field in Odoo

Abid Patel
21-Oct-2024 Updated : 21-Oct-2024

Learn how to create a searchable non-stored compute field in Odoo to enhance dynamic data retrieval in your models.

Searchable Non-Stored Compute Field in Odoo

In Odoo, computed fields are versatile tools that allow you to derive values based on other fields in your models. While many computed fields are stored in the database, sometimes you might want to create a non-stored computed field that is also searchable. This can be particularly useful when you need to display information dynamically but do not want to save it permanently. In this blog post, we will discuss how to create a searchable non-stored computed field in Odoo.

What is a Non-Stored Compute Field?

A non-stored computed field is a field that calculates its value on-the-fly and does not save that value to the database. This means that the value is recomputed each time it is accessed. Non-stored fields are useful for displaying calculated values that do not need to be persistent and can change frequently.

Making a Computed Field Searchable

To make a computed field searchable in Odoo, you need to ensure that it is defined as a computed field and add the search attribute to the field definition. Let's go through the steps to create a searchable non-stored compute field.

Step 1: Define Your Model and Non-Stored Compute Field

python

from odoo import models, fields, api

class MyModel(models.Model):
    _name = 'my.model'

    name = fields.Char("Name")
    description = fields.Text("Description")
    search_field = fields.Char("Searchable Field", compute="_compute_search_field", search="_search_search_field", store=False)

    @api.depends('name', 'description')
    def _compute_search_field(self):
        for record in self:
            record.search_field = f"{record.name} - {record.description}"

In this example, we define a model called my.model with a non-stored computed field called search_field. The _compute_search_field method concatenates the name and description fields into a single string. Notice that we set store=False to indicate that this field is non-stored.

Step 2: Define the Search Method

To make the computed field searchable, we need to implement a search method. This method will define how the search is performed on the search_field.

python

    def _search_search_field(self, operator, value):
        if operator in ('=', '!='):
            return [('name', operator, value)] + [('description', operator, value)]
        return [('name', operator, value)]

In the _search_search_field method, we handle the search logic. Here, we allow searching by either the name or description fields based on the operator provided. You can customize this method to implement more complex search logic as required.

Step 3: Updating the View

Next, you need to add the non-stored computed field to your views, so users can see it and use it in their searches:

xml

<record id="view_my_model_form" model="ir.ui.view">
    <field name="name">my.model.form</field>
    <field name="model">my.model</field>
    <field name="arch" type="xml">
        <form>
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="description"/>
                    <field name="search_field" readonly="1"/>
                </group>
            </sheet>
        </form>
    </field>
</record>

This XML code adds the search_field to the form view, making it visible to users. Setting readonly="1" prevents users from modifying the value directly since it is a computed field.

Conclusion

Creating a searchable non-stored compute field in Odoo is a great way to enhance the user experience by allowing dynamic calculations without permanently storing those values. By following the steps outlined in this blog, you can easily implement searchable computed fields in your custom modules, offering users powerful data retrieval capabilities without compromising on performance.

Make a Comment

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