Help

3

Change default sort order

Avatar
Michael St-Georges

Hi,

 

Is there a way to change the default sorting column for a treeview (sales.order specifically)? I have a custom field I have set up and would like the sorting to default to this column.

 

Thanks all

Avatar
Discard
3 Answers
10
Avatar
Ray Carnes
Best Answer

VIA UI:

As of Odoo 8 you can simply add a default_order attribute in the view definition, to override the default ordering of the model. The syntax for this attribute is the same as the `_order` attribute of the model in Python.

Example:

<tree default_order='date_expected, picking_id, sequence'>
5 Comments
Avatar
Discard
Avatar
Raphael
-

I tried this under the edit tree view - Manufacturing Orders (Edit Tree View (Developers Mode)) by adding the following line It now reads: It comes up with a ValidateError Field(s) `arch` failed against a constraint: Invalid view definition

Avatar
Ray Carnes
-

Post a new question and expand 'I tried this' into steps with screenshots, so we can understand what you did and where you went wrong.

Working for four years with Odoo now and I never knew this. Thanks Ray, great tip! Upvoted.

@Ray : I tried this method and it is working perfectly for ascending order.Can you please tell how to do it in descinding order ?Thanks

<tree default_order='date desc'> Does the trick for me.(For descinding order)

5
Avatar
Cyril Gaspard (GEM)
Best Answer

Hi,

in your class inherited of you module,

add :

class NameClass(....):

    _inherit = "name.class"

    _order = "custom_field_name, other_field ..." (you can add option asc or desc too)

Bye

3 Comments
Avatar
Discard
Avatar
Michael St-Georges
-

Thanks for the prompt answer. I kind of hoped there was a GUI way of doing things since this is for just that one very very specific install, but I guess there isn't.

Avatar
Andre Leander
-

How can I give spesific order to tree view?? In your answer we must inherit class. Can we do it?

Avatar
Cyril Gaspard (GEM)
-

see https://www.odoo.com/forum/help-1/question/order-one2many-lines-29222#answer-75504

0
Avatar
Darshan Patel
Best Answer

Instead of changing from the _order attribute from Python File.

You can just change the default_order attribute of Tree View (This way you can have different order by based on each tree view for that specific model)

<tree default_order="name">

- And can inherit base view with following syntax (Descending example)

<xpath expr="//tree" position="attributes">
        <attribute name="default_order">name desc</attribute>
</xpath>

Avatar
Discard