Retrieve highest value of all records

Hi everyone and thank you in advance. I am new to Tryton and struggling with some basics I guess.

I am trying to implement a default value for a new laboratory request. The class working example is:

class LabTest(ModelSQL, ModelView):
    'Lab Test'
    __name__ = 'lab.test'

    order = field.Integer('Order number')

I want the “Order number” field to suggest the highest recorded request number + 1 on a new input.

I do understand that this is realised using default methods such as:

    @classmethod
    def default_order(cls):
        highest_number = max(all_request_numbers)
        next_order = highest_number + 1
        return next_order

However, I fail to understand how I can retrieve an iterable list of all request numbers (“all_request_numbers”) that are available in all records.

Thank you very much for your help!

For this you should use a sequence. The sequence will generate the next number.

An example can be found in Party module, where if a checkbox in Party configuration is checked, then a new party will get it’s code from the configured sequence.

This is not working on concurrent environment. And more over the default value is retrieved when the client is opening the form so when the form is saved this default value may no more be true.

But I guess you do not care about gaps in the sequence so Retrieve highest value of all records - #2 by dotbit is the proper solution.
Otherwise you will have to use a transition to set the number and prevent deleting numbered records.

Thanks to both of you.

I believe that using sequences is the right way to do it. I wasn’t aware of them since I just started with tryton.

Since we will be transitioning from an ancient laboratory system we won’t be able to start from test #1, but need to make a cut with the data and continue our lab management with tryton. That’s why I was thinking of the “max value” solution.

But since one can configure start values with sequences, they should be quite appropriate.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.