Skip to Content
Menu
This question has been flagged
2 Replies
6766 Views

I want to sort my lists by a computed field "due_days" (number of days - computed from "due_date").

I could do this by setting "store=True" on due_days, but then the compute method depends on the current date. How can I then be sure, that the value is re-computed when the current date changes?

It would also be easy to implement a new search method. Searching on due_days is the same as searching on due_date with ASC/DESC order reversed. But how can I tell Odoo to sort by this order?

Avatar
Discard
Author

I'm aware of the @api.depends decorator, but as far as I can see, it can only be triggered by other fields. As the "due_days" field depends on both "due_date" and the current date, we also need to re-calculate due_days when the current date changes.

Maybe I wasn't clear on the sorting, but I want the user to be able to choose the sorting when they see a list.

Best Answer

Hi,

you can take the help of scheduled actions in this scenario, write a scheduled job which will update the desired field every day, if the number of records is so large in number then write a logic to slice the records and update them over the day. Hence it'd run every day and change the records.

There could be a better solution for this but without storing the field in DB Odoo won't allow sorting the list.

Thanks.

Avatar
Discard
Best Answer

Assuming you are using odoo8, you can decorate you method for your computed field with @api.depends("field.name.here")

And to sort search results for a model, in that model's class (python) you can add _order = "fieldname ASC, otherfieldname DESC"

Avatar
Discard