forvas
forvas

Reputation: 10189

How to order by two fields in Odoo 8?

I have a recordset of partners. Now I want to order this recordset by two attributes. One of this attributes is a simple Char, but the other one is a Many2one, which is a problem.

Example (suppose variable partners is the recordset):

The problem is: I need to order by two fields, as I wrote above. For that it seems that I have to use itemgetter, but:

I also tried to link two sorted in a row, this way:

partners.sorted(key=lambda r: r.name).sorted(key=lambda r: r.country_id.id)

But it is not working neither, because the second sorted function messes up the result of the first one.

Can anyone help me with this, please?

Upvotes: 3

Views: 4011

Answers (2)

Kenly
Kenly

Reputation: 26708

I think you need to sort using country name.
Using your last code line and calling sort one time it will solve the problem:

partners.sorted(key=lambda r: (r.name, r.country_id.name))

Upvotes: 3

CZoellner
CZoellner

Reputation: 14778

What about

partners.sorted(key=lambda p: (p.name, p.country_id.id))

Upvotes: 3

Related Questions