Enable Dark Mode!
By: Rahna Rasheed

How to Add a Search Filter for Computed Fields in Odoo 17


We must first understand the purpose of Odoo's computed fields. Computed fields are used when we need to retrieve computed or calculated values from other fields. i.e., functions can be used to calculate field values rather than reading values from a database. When we wish to find the total amount of a product, we can multiply the product price by the total number of products, which is an example of the computed field in action. That is Total = Product Price x Number of Products.

Compute fields in Odoo are not kept in the database by default.

Therefore, we might occasionally need to search or filter using these computed fields.

This blog will mainly cover How to Add a Search Function for a Non-Stored Compute Field in Odoo. The computed fields in Odoo can be stored in two different ways. By adding store=True, we are able to make the field store. Using a search function and setting it on the field is an additional technique.

Thus, we wish to filter according to the expired sale. We have included a Date field called expiration_date and a Boolean field called is_expired for that purpose. As demonstrated by the code provided below.

is_expired = fields.Boolean(string="Is Expired",                  compute='_compute_is_expired',store=True)

Here, we have included a search function in addition to the computing field in the is_expired field. The compute and search functions are defined in the Python code that is provided below. Three arguments must be passed to the search function: self, operator, and value.

is_expired = fields.Boolean(string="Is Expired", compute='_compute_is_expired',search = '_search_is_expired')
def _search_is_expired(self, operator, value):
   today = fields.Date.today()
   records = self.env['sale.order'].search([('validity_date', '<',          today)])
   return [('id', 'in', records.ids)]

In the XML, we have the filter as you can see in the below code, and also define the search_view_id.

<record id="is_expired_view_search_inherit_quotation" model="ir.ui.view">
   <field name="name">is.expired.search.view</field>
   <field name="model">sale.order</field>
   <field name="inherit_id" ref="sale.view_sales_order_filter"/>
   <field name="mode">primary</field>
   <field name="priority">32</field>
   <field name="arch" type="xml">
   <xpath expr="//filter[@name='my_sale_orders_filter']" position="after">
       <filter name="is_expired" string="Is Expired"  domain="[('is_expired',  '=', True)]"/>

How to Add a Search Filter for Computed Fields in Odoo 17-cybrosys

This is how to include a search filter in Odoo for computed fields. We will be writing the code to compute the value in the compute function. Also, the search function specifies what should be returned. When store=True is selected, the data is saved to the database.

To read more about How to Add a Search Filter for Computed Fields in Odoo 17, refer to our blog How to Add a Search Filter for Computed Fields in Odoo 17

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message