跳至内容
菜单
此问题已终结
1 回复
13484 查看

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


形象
丢弃
编写者 最佳答案

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!


形象
丢弃
相关帖文 回复 查看 活动
1
1月 19
8023
0
12月 24
598
0
12月 24
544
2
7月 24
2010
2
3月 22
11298