问题
I'm getting crazy for what I believe it is a really silly matter. I need to render the result of an array in alphabetical order:
<tr t-foreach="o.order_line" t-as="l">
<td>
<span t-field="l.name"/>
</td>
should I use a SQL query SELECT * FROM table ORDER BY
l.nameDESC
?
but it seams too complicated, I have the feeling there is a simple condition to render it correctly...
any help highly appreciated! Thanks!
回答1:
Have a look at this
You could set a new variable to order_line.sorted() and then iterate on the new variable
For sorting have a look at Odoo reference
回答2:
I have just faced this problem and I was able to solve it with sorted
function, as @Alessandro Ruffolo wrote. You have to pass the right parameters to that function, in your case it would be:
<tr t-foreach="o.order_line.sorted(key=lambda r: r.name, reverse=True)" t-as="l">
<td>
<span t-field="l.name"/>
</td>
...
</tr>
回答3:
Actually you cannot use ".sort()" for one main reason:
- ".sort()" on a list sorts the list in place, returning
None
The best you can do is to use sorted
, which does not modify the iterable you pass to it but returns its sorted value. Like this:
<tr t-foreach="sorted(o.order_line, key=lambda x: x.get('A_FIELD_TO_SORT_UPON')" t-as="l">
The key
could be any fuction that returns the value to be used for sorting.
See some more examples on sorted
usage here.
回答4:
If you want to sort item in Odoo Report this is will work!!!
<tr t-foreach="get_room_used_detail(data['form']['date_start'],data['form']['date_end']).sorted(key=lambda x: x.checkin)" t-as="info">
来源:https://stackoverflow.com/questions/35474335/odoo-qweb-report-lines-in-alphabetical-order