Query vs Search for a function field

Hi,
I need to add a field on product model to get the last cost price of the previous month. (taken from product_cost_history)
Some questions:

  • I intend to use a function field, do think it’s the best choice ?
  • To get the last cost price of the previous month, is it better to use a query or is it possible to do this with a search ?
    Thanks for advices/help ! :slight_smile:

If you want it to be performant, you should go for a SQL query because ORM can do that only one product at a time.
With SQL query, you can use a LAST_VALUE window function. You can find an example in Currency.currency_rate_sql.

1 Like