Reputation: 10189
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):
partners.sorted(key=lambda r: r.name)
(with Char
no problem).partners.sorted(key=lambda r: r.country_id)
(with Many2one
, you must add an attribute of the other model).partners.sorted(key=lambda r: r.country_id.id)
(now we have indicated an attribute of res.country
, the recordset is ordered rightly).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:
partners.sorted(key=itemgetter('name', 'country_id'))
(country_id
is ignored, as when I tried to order by only this one above).partners.sorted(key=itemgetter('name', 'country_id.id'))
(the error message is that country_id.id
is not a field of res.partner
).partners.sorted(key=itemgetter('name', 'phone'))
(because both fields are Char
).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
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
Reputation: 14778
What about
partners.sorted(key=lambda p: (p.name, p.country_id.id))
Upvotes: 3