✅ What is it?
✅ Syntax
📌 Realistic Odoo Example
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 |
🧠 Use Cases
- Filter sales orders with computed margin
- Search for overdue tasks with computed delay
- Filter products based on computed stock forecast
✅ Summary Table
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 |