콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
10191 화면


Hi,

I'm trying to edit report_saleorder_document to display each variant on a product's template separately.


I'm using the code:

<t t-foreach="doc.order_line" t-as="line">
    <span t-field="line.name"/>
    <span t-field="line.product_template_attribute_value_ids"/>
</t>
and the result is:

T-Shirt   Blue,Size XL,Cotton

but how can I retrieve ONLY the second variant (Size) to get a result like this?

T-Shirt   Size XL


NOTE ****************************************************************************************************

I have already tried to split the text, but it's not working

<t t-set="splited" t-value="line.product_template_attribute_value_ids.split(',')"/>
<t t-esc="splited[2]"/>

I get the error

AttributeError: 'product.template.attribute.value' object has no attribute 'split'
아바타
취소
베스트 답변

 Hi,

If you are sure that you will get more than one attribute than you can use this code to get your second index attribute value:

<t t-foreach="doc.order_line" t-as="line">
    <span t-field="line.name"/>
    <span t-field="line.product_template_attribute_value_ids[1].name"/>
</t>

아바타
취소
작성자

I am not sure that there will be more than one attribute, would this t-if work?

<t t-foreach="doc.order_line" t-as="line">

<span t-field="line.name"/>

<t t-if=""line.product_template_attribute_value_ids[1].name"/>

<span t-field="line.product_template_attribute_value_ids[1].name"/>

</t>

That would work. But your use case looks different though. From what I am getting is that, if your product attribute is "Size XL" only then you want to show it otherwise not. So I think better approach would be to specifically check that condition. For example:

<t t-foreach="doc.order_line" t-as="line">

<span t-field="line.name"/>

<t t-if=""line.product_template_attribute_value_ids.filtered(lambda i: i.name == 'Size XL')"/>

<span">Size XL</span>

</t>

</t>

What this code does is it checks for if attribute with name "Size XL" is present in all the attributes for that product. If it does it will write Size XL in front of it. Otherwise it will just skip over for that product.

베스트 답변

Hi Hugo:

Try this in place of the 2 lines of code you have listed.

Since this is a many2many relationship, the "mapped" function will retrieve a mapped list of the attribute names and "1" will retrieve the 2nd attribute since the index numbers start at 0.

<span t-esc="line.product_template_attribute_value_ids.mapped('name')[1]"/>
아바타
취소
작성자

Both your solution and Deepak's solution worked really well, thanks

But what is the difference between:

<span t-field="line.product_template_attribute_value_ids[1].name"/>

<span t-esc="line.product_template_attribute_value_ids.mapped('name')[1]"/>

Is there a benefit in using mapped ?

'mapped' and 'filtered' are iterations method provided by odoo. My solution was directly accessing the second index(place) value of Attributes therefore little quick but almost negligible in term of speed.

관련 게시물 답글 화면 활동
1
7월 24
1470
1
3월 15
550
1
3월 15
9975
0
3월 15
3711
0
3월 15
4139