Create sort_ function for functional field

Hi,

I was reading the docs about functions to order funtional fields [6], [7] and reviewing some examples on tryton git [0], [1], [2], [3], [4], [5] but I could not find and example on how to use for a specific case that have.

Basically I have a model to record the gathering moves of every month and a group. Every group have more than 5 people, it will vary:

Moves:

Group, Month, Date,

Moves Lines:

Move, Party, Amount

I added a custom field that calculate on Party that get the sum of the move lines and it works fine, but I wish to order from highest to lowest.

Where could I find an example of how can order these type of field?

Thanks in advance!

[0] https://github.com/tryton/party/blob/develop/party.py#L100
[1] https://github.com/tryton/account/blob/f8e616ba2aa40502e2f478c8506805adeee0f424/move.py#L143
[2] https://github.com/tryton/product/blob/develop/product.py#L279
[3] https://github.com/tryton/purchase_request_quotation/blob/5d151e6896a0a1374f3b4f428425aab0515024de/purchase.py#L320
[4] https://github.com/tryton/timesheet/blob/develop/line.py#L354
[5] https://github.com/tryton/party/blob/b184b5996b7e983e0ce4beb75216962c532ef3b4/address.py#L255
[6] Fields — trytond 5.3 documentation
[7] Fields — trytond 5.3 documentation

There is no such example because it would be resource consuming.

Any way, you have to make a join (adding to tables argument) with a query the will return the party id and the SUM. Then you return the clause to order with the SUM column of your query.

2 Likes

I just updated the training module with an example of an “order” method implementation:

Search for “Defining a order method”

Edit : For your use case, you would have to add an entry in the “tables” dict which is a full sub-query.

3 Likes