Odoo Help


[QWeb] get variable value­ from loop

Vasiliy Birukov
on 1/29/15, 4:29 AM 3,520 views

Is it possible get variable value from loop?

For example:

            <t t-set="flag" t-value="False"/>
            <!-- flag now False -->
            <tr t-foreach="o.lines" t-as="line">
                <td><span t-field="line.name"/></td>
                <td><span t-field="line.qty"/></td>
                <t t-set="flag" t-value="True"/>
                <!-- flag now True -->
            <!-- flag again False -->

Variable flag inside loop is different from variable flag outide. How send information outside loop?

A patch and some ideas on how to calculate totals have been added to the github issue. https://github.com/odoo/odoo/issues/4461#issuecomment-72199618

Stephen Mack
on 1/30/15, 8:56 AM

Stephen Mack

--Stephen Mack--

| 8 8 9
Santiago, Chile
--Stephen Mack--

Some people call me a karma whore, I only ask for 10 points if you like my answer and 15 if I happen to get it correct.

Stephen Mack
On 1/29/15, 8:21 AM

There is a GitHub Issue #4461 on this problem.

UPDATE: A patch to v8 was released and some good programming suggestions were made by Olivier on how to avoid using loops.

Updated my answer with the patch.

Stephen Mack
on 1/30/15, 9:00 AM

Vasiliy Birukov

--Vasiliy Birukov--
| 7 6 8
Russian Federation
--Vasiliy Birukov--

Help develop and implement any project on base Odoo.

Vasiliy Birukov
On 1/30/15, 12:04 AM

Thank you Stephen Mack !

As a workaround we can use:

1) If you can insert output in last iteration then use like:

                <t t-set="total" t-value="0" />
                <t t-foreach="o.order_line" t-as="line">
                    <t t-set="total" t-value="total+line.quantity" />
                    <t t-if="line_last">
                        <t t-esc="total" />

​​2) If you can't insert output in loop then use like:

                <t t-set="total" t-value="sum([line.quantity for line in o.order_line])" />

Those are some ugly workarounds. But thanks for posting them.

Stephen Mack
on 1/30/15, 6:00 AM

I agree :(

Vasiliy Birukov
on 1/30/15, 7:49 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 1/29/15, 4:29 AM
Seen: 3520 times
Last updated: 5/6/16, 5:39 AM