Skip to Content
Menu
This question has been flagged
1 Reply
12948 Views

Hello all,

I'm in odoo 8.

I use this line in a report template to sort products by default code :

<t t-foreach="docs.sorted(key=lambda x: x.default_code)" t-as="product">


It works well, but products are sorted like this (product with first letter in lowercase at the end...) :

AA0001
BB0001
WW0009
aa0002

bb0001

But I would want products sorted like this instead :

AA0001

aa0002
BB0001
bb0001
WW0009


or like this :

AA0001

aa0002
bb0001

BB0001
WW0009


Thanks to help!


EDIT #1

I have tried many ways to include the lower() function in my qweb line, no success. How should I use the lower() function?

For example :

 <t t-foreach="docs.sorted(key=lambda x: (x.default_code).lower())" t-as="product">
<t t-foreach="docs.sorted(key=lambda x: x.default_code.lower)" t-as="product">
<t t-foreach="docs.sorted(key=lambda x: lower(x.default_code))" t-as="product">
<t t-foreach="docs.sorted(key=lambda x: x.default_code.lower())" t-as="product">

etc...


Avatar
Discard
Author Best Answer

I managed to fix this problem!

The problem was that the default_code field on product.template model is a not stored related field. So, sorting records according to this field didn't work properly.

So, I have overrided the default_code field to store it in the database :

from openerp import models, fields, api
class product_template(models.Model):
    _inherit = "product.template"
      
    default_code = fields.Char(related='product_variant_ids.default_code',
        string='Internal Reference of the first variant of this product', store=True)


After this, my qweb line works properly! Even with the lower() function in it!

<t t-foreach="docs.sorted(key=lambda x: x.default_code.lower())" t-as="product">


Thanks all!


Avatar
Discard
Related Posts Replies Views Activity
1
Jan 19
7322
2
Jul 24
650
2
Mar 22
10543
1
Mar 21
2308
1
Feb 24
10990