erica
erica

Reputation: 11

odoo Qweb report lines in alphabetical order

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 BYl.nameDESC? but it seams too complicated, I have the feeling there is a simple condition to render it correctly...

any help highly appreciated! Thanks!

Upvotes: 1

Views: 6169

Answers (4)

Sophanon Nun
Sophanon Nun

Reputation: 1

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">

Upvotes: 0

forvas
forvas

Reputation: 10189

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>

Upvotes: 2

simahawk
simahawk

Reputation: 2431

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.

Upvotes: 1

Alessandro Ruffolo
Alessandro Ruffolo

Reputation: 1575

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

Upvotes: 2

Related Questions