[Odoo][Qweb]字典foreach,打印密钥和值

2024-04-27 15:13:29 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法在循环中从python字典中打印键和值? 例如,如果我有一个函数返回字典:

def get_informations(self):
    mydico={'myfirstkey':1,'mysecondkey':2}
    return mydico

然后,在Qweb报告中:

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

如何打印键和值?

谢谢

2015年12月7日更新:

谢谢你回来。 没错,当我把

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

它起作用了,我有这样的东西:

my    first
my2   second

但当我在foreach中使用一个输出完全相同的函数时,qweb无法将其分离,我有:

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

所以我决定换一种方式:

在我的继承报告中:

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

sale.ordermodels inherit中:

@api.one
def get_amount(self, taxeNom):
    total=0
    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)

Tags: nameselfgetdocmyastrtd
2条回答
  • $全部

正在迭代的对象

  • $作为价值

当前的迭代值,对于列表和整数与$as相同,但是对于映射,它提供值(其中$as提供键)

  • Warning

$as will be replaced by the name passed to t-as

此链接的引用:https://www.odoo.com/documentation/8.0/reference/qweb.html

最后我明白了如何使用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']"/>
  <td>
    <strong>
      <p>
        <t t-esc="name"/>
      </p>
    </strong>
  </td>
  <td class="text-right">
    <t t-esc="total" t-esc-options='{"widget": "monetary", "display_currency": "res_company.currency_id"}'/>
  </td>
</tr>

相关问题 更多 >