When working with Odoo, it’s common to create computed fields—fields that calculate their value automatically instead of being entered manually. These are extremely useful for things like calculating totals, checking conditions, or summarizing information from other fields.
But here’s the catch: computed fields are not stored in the database by default. That means you can see them on the form or list view, but you can’t search or filter records using them unless you take an extra step.
In this article, we’ll walk through how to make computed fields searchable in Odoo 19 by adding a search filter.
Stored vs Non-Stored Computed Fields
Odoo lets you decide how computed fields behave:
- Stored Computed Fields (store=True)
The value is saved in the database. This makes it searchable and filterable automatically.
- Non-Stored Computed Fields
The value is calculated on the fly whenever you access it. These are lighter on storage but not directly searchable. To make them searchable, you need a custom search method.
Example: Filtering Employees Who Are on Probation
Let’s say you want to add a filter to quickly find employees who are still in their probation period. An employee is considered “on probation” if their hire date is within the last 90 days.
We’ll add a computed Boolean field is_on_probation, and then make it filterable.
Python Code
# -*- coding: utf-8 -*-
from odoo import api, models, fields
from datetime import timedelta
class Employee(models.Model):
_inherit = 'hr.employee'
first_contract_date = fields.Date(string='First Contract Date')
is_on_probation = fields.Boolean(
string="On Probation",
compute="_compute_is_on_probation",
search="_search_is_on_probation"
)
@api.depends('first_contract_date')
def _compute_is_on_probation(self):
"""Compute the probation date"""
today = fields.Date.today()
for emp in self:
if emp.first_contract_date:
probation_end = emp.first_contract_date + timedelta(days=90)
emp.is_on_probation = today <= probation_end
else:
emp.is_on_probation = False
def _search_is_on_probation(self, operator, value):
"""Custom search to filter employees still in probation period"""
today = fields.Date.today()
probation_limit = today - timedelta(days=90)
employees = self.env['hr.employee'].search([
('first_contract_date', '>=', probation_limit)
])
return [('id', 'in', employees.ids)]
Here’s what’s happening:
- _compute_is_on_probation checks if the employee’s first contract date is within 90 days.
- _search_is_on_probation builds a domain that finds those employees when you use the filter.
Adding the Filter in the Search View
Now let’s add a filter button in the employee search view and the First Contract Date in the Employee Form view:
<record id="view_employee_filter_probation" model="ir.ui.view">
<field name="name">hr.employee.search.probation</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_filter"/>
<field name="arch" type="xml">
<xpath expr="//filter[@name='activities_today']" position="after">
<filter name="is_on_probation"
string="On Probation"
domain="[('is_on_probation', '=', True)]"/>
</xpath>
</field>
</record>
<record id="hr_employee_view_form_inherit_custom" model="ir.ui.view">
<field name="name">hr.employee.form.inherit.custom</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='department_id']" position="after">
<field name="first_contract_date"/>
</xpath>
</field>
</record>
With this in place, users can open the employee search view and click On Probation to instantly see who’s still within their probation period.
Conclusion
Computed fields are great for automating business logic, but making them searchable requires a little extra work. In Odoo 19, you have two main options:
- Use store=True if the value doesn’t change often and can safely be saved in the database.
- Use a custom search method if the field should always be calculated dynamically.
By combining computed fields with search filters, you can give users quick access to insights that matter most to your business.
To read more about How to Add a Search Filter for Computed Fields in Odoo 18, refer to our blog How to Add a Search Filter for Computed Fields in Odoo 18.