FTK FTK - 2 years ago 476
Python Question

[Odoo][Qweb]Dictionary foreach, print key and value

Is there a way to print key and value from a python dictionary in loop Qweb?
For example, if I have a fuction that return a dictionary:

def get_informations(self):
return mydico

And then, in Qweb report:

<t t-foreach="doc.get_informations()" t-as="l">
<td class="text-right">
<t t-esc="l"/>
<td class="text-right">
<span t-esc="l_value"/>

How could I print the key and the value ?


Update 07/12/15:

Thank you for your return.
Exactly, when I put

<t t-foreach="{'my': 'first', 'my2': 'second' }" t-as="v">

It works, I have something like:

my first
my2 second

But when I use a function in foreach, with exactly the same output, qweb can't separate it, and I have:

{'my': 'first', 'my2': 'second' }
{'my': 'first', 'my2': 'second' }

So I decided to do another way:

In my inherit report:

<t t-foreach="doc.tabTaxes" t-as="v">
<span t-esc="v.name"/>
<span t-esc="doc.get_amount(v.name)[0]"/>

In sale.order models inherit :

def get_amount(self, taxeNom):
for ligne in self.order_line:
for taxe in ligne.tax_id:
if (taxeNom == taxe.name):
try: total += ligne.price_reduce * (taxe.amount/100.)
except: total +=0
return "{0:.2f}".format(total)

Answer Source

Finally I understood how to work with V9 :

<tr t-foreach="o.get_list_taxe(o.id)[0]" t-as="taxe">
  <t t-set="name" t-value="taxe['name']"/>
  <t t-set="total" t-value="taxe['total']"/>
        <t t-esc="name"/>
  <td class="text-right">
    <t t-esc="total" t-esc-options='{"widget": "monetary", "display_currency": "res_company.currency_id"}'/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download