How to order by a boolean field in Odoo?

2.4k views Asked by At

I can't understand the behaviour of the Odoo API. It's driving me crazy.

As you may know, if you want to order a model by some of its fields, you have to modify the attribute _order of the model. It works fine.

Now, suppose you want to order a specific view by some fields without altering the normal order of the model (this means: order a specific view by some fields and the rest of the views of the same model by other fields).

In this case, you have to use XML code, specifying the attribute default_order in the kanban or tree tag, depending on which view you are modifying.

I've used this in the past succesfully. So I wanted to apply this again to a kanban view (this time I want to order by a boolean field, a char and another char).

Now my surprise: ordering by a boolean field reverses the order of the next fields if it values False.

e.g. We have 6 records, which we want to order by a boolean, the name, and surname; in a kanban view with default_order:

  • True, John, Doe
  • True, Jane, Doe
  • True, John, Xoe
  • False, John, Die
  • False, Jane, Die
  • False, Jane, Xie

So in the example, in the kanban view we'll see them ordered this way:

  1. True, Jane, Doe
  2. True, John, Doe
  3. True, John, Xoe
  4. False, John, Die
  5. False, Jane, Xie
  6. False, Jane, Die

As you can see, if the boolean values True, the records are ordered by boolean, name and surname, which is what I want. But if it values False, the records are ordered by boolean, name DESC, surname DESC.

What's going on? Did you experience the same? How can I order by a boolean, then a char, and then another char??

1

There are 1 answers

3
Adrian Merrall On BEST ANSWER

One method I use to to odd sortsis use the context to control it. In the window action for the kanban view, add a context like

<field name="context">{'do_my_special_sort': True}</field>

and then on the model, override the search method, check for this flag in the context and set the sort order.