Enable Dark Mode!
how-to-add-a-search-filter-for-computed-fields-in-odoo-16.jpg
By: Aneesh Akramannil

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

Technical Odoo 16 Odoo Enterprises Odoo Community

First, it's important to understand why Odoo uses calculated fields. Computed fields are utilized when we need to obtain computed or calculated values from other fields. In other words, rather than retrieving a value from a database, values for a field can be calculated using functions. The computation of a product's total amount, which is obtained by multiplying the product price by the total number of items, is an example of a calculated field. i.e., Total is equal to the product's price times the number of products.

Compute fields are not kept in the database by default in Odoo 16. As a result, there can be circumstances in which we need to use these computed fields to search or filter.

This blog post will mostly cover how to Add a Search Function for a Non-Stored Compute Field in Odoo 16 to accomplish that.

In Odoo, the computed fields can be stored in two different ways. By setting store=True, we can create the store field. Additionally, you can use a search function by setting it on the field.

I'll illustrate this using the is_expired field in the sale order as an example.

We don’t have the option to filter by is_expired field. Let us add it by inheriting the sale order 

how-to-add-a-search-filter-for-computed-fields-in-odoo-16-1-cybrosys

class SaleOrder(models.Model):
   _inherit = "sale.order"
   is_expired = fields.Boolean(string="Is Expired",                  compute='_compute_is_expired',store=True)

Here, we have added the field attribute store=True to the field. (NB: If you maintain store=True, the only time your compute function will run is if the value of the depends on field changes).

The second method to search using a computed field is to add a search function to the computing field as demonstrated in the code provided below.

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)]

We need to inherit the search view in XML and add filters to it.

<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)]"/>
   </xpath>
</field>
</record>

Likewise, we can incorporate a search feature not only for boolean fields but also for computed fields in other categories of fields.

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 adding a search filter for computed fields in Odoo 15, refer to our blog How to Add a Search Filter for Computed Fields in Odoo 15


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



0
Comments



Leave a comment



whatsapp
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message