This question has been flagged
2 Replies
8268 Views


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'
Avatar
Discard
Best Answer

 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>

Avatar
Discard
Author

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.

Best Answer

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]"/>
Avatar
Discard
Author

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.