How to set default descending sorting order in a tree view?

I am trying to set the sort order of a tree view.
Now I have the oldest record first:

And the following XML:

<tree string="Inspections" sequence="id" tree_state="1">
    <field name="inspection_datetime" select="1" widget="date"/>
    <field name="inspection_datetime" width="50" select="2" widget="time"/>
    <!-- SKIPPED -->
    <field name="id" tree_invisible="1"/>

I have no idea — how to set a descending order of ids???


ordering is defined either in python side (for default value) or in the action.

1 Like

Well, that’s pitty, but ok.
And thank you so much!
It works perfectly now.

You can also click on the column header to sort on its value (multiple times to toggle the direction).

That is exactly the thing my customer is complaining about: “Why on Earth I should click three times on the header to see the most recent record on top?!” :slight_smile:

It’s something that they should do only if the default do not suit them in one rare occurence.
Otherwise as @sergyo said you can also adapt the default to their liking.

Yes, my customer always need the most recent record on top, that is not the default behavior.
I did exactly as it was proposed by @sergyo! And I like it. I didn’t understand that way from the docs. He pointing me to the example and — that was what I need.
I ended with the following solution:

        <!-- # Open window with Inspections -->
        <record model="ir.action.act_window" id="act_inspections">
                <field name="name">Inspections</field>
                <field name="order"
                eval="[('id', 'DESC'), ('assess_datetime', 'DESC')]"
                <field name="res_model">mymodule.assessment</field>
1 Like

Setting an default order on ModelSQL._order would remove the need to define it on all actions and also applies to view without action.

You do not need the second clause because ‘id’ is unique so it define by itself the full order.

1 Like

Yes, I know about id, I just left both for having the ability to switch between these two ways to which it would be better for the customer. And we finally agreed on the id version, so assess_datetime is excessive and should be removed.

Thank you, I will try ModelSQL._order with another view…

I used the following declaration:

class SaleAnalytics(ModelSQL, ModelView):
    # description (mandatory on first declaration)
    'Sale Analytics'

    # Internal class name. Always used as a reference inside Tryton
    # default: '<module_name>.<class_name>' on Tryton
    # becomes '<module_name>_<class_name>' in the database
    __name__ = ''

    _order_name = 'report_date'
    _order = [('report_date', 'DESC')]

I shared it as it seems to work fine. For those who follow my path…

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