viewqwebodoo-16

Odoo16 qweb - convert float to int / loop through float


Good morning, in odoo 16 I created a view for printing product labels. Now I'm trying to make the number of labels match the amount of on hand. I'm using foreach, but the on hand value is float and foreach throws an error.

TypeError: 'float' object is not iterable

How do I convert float to int in qweb? I tried to use range(qty) in foreach, but it doesn't help

<t t-name="xxNAMExx">
  <t t-set="qty" t-value="product.qty_available"/> //float -> int??
  <t t-foreach="qty" t-as="item"> //<t t-foreach="range(qty)" t-as="item"> 
    <div>CONTENT</div>
  </t>
</t>

Solution

  • You should convert the qty_available float-value to integer using int():

    <t t-name="xxNAMExx">
      <t t-set="qty" t-value="int(product.qty_available)"/>
      <t t-foreach="range(qty)" t-as="item">
        <div>CONTENT</div>
      </t>
    </t>