How to Implement search Parameter in Odoo?
✅ What is it?
When you define a computed field in Odoo, you may also want to make it searchable in the UI (tree, form, search view). However, since the value is not stored in the database (store=False), Odoo cannot natively perform SQL-level filtering.
To make such a computed field searchable, you provide a custom search method using the search= parameter.
Syntax:
fields.FieldType('Field Label', compute='_compute_method', search='_custom_search_method')
✅ Realistic Odoo Example
🎯 Scenario:
You want to define a computed datetime field called delay_alert_date which triggers an alert if delivery is delayed, and you want this field to be searchable in the UI (e.g. filter records where delay is after a given date).
✅ Code:
from odoo import models, fields, api
class StockPicking(models.Model):
_inherit = 'stock.picking'
delay_alert_date = fields.Datetime(
'Delay Alert Date',
compute='_compute_delay_alert_date',
search='_search_delay_alert_date'
)
def _compute_delay_alert_date(self):
for record in self:
if record.scheduled_date and record.date_done:
if record.date_done > record.scheduled_date:
record.delay_alert_date = record.date_done
else:
record.delay_alert_date = False
@api.model
def _search_delay_alert_date(self, operator, value):
# Custom search logic
pickings = self.search([
('scheduled_date', '<', value),
('date_done', operator, value)
])
return [('id', 'in', pickings.ids)]
🔍 Explanation:
Component | Purpose |
compute='_compute_delay_alert_date' | Calculates the field's value dynamically |
search='_search_delay_alert_date' | Enables search/filter on this field |
_search_delay_alert_date method | Performs the actual search based on logic you define |
⚠️ Without defining search=, users cannot filter based on this computed field in views or filters.
🧠 Use Cases
- Filter sales orders with computed margin
- Search for overdue tasks with computed delay
- Filter products based on computed stock forecast
✅ Summary
Concept | Description |
search= param | Points to custom method for searching computed fields |
When needed | For non-stored (store=False) computed fields |
Returns | A domain like [('id', 'in', matching_ids)] |
Benefit | Makes dynamic fields searchable in UI |