4

How reduce string length in qweb?

Example:

<p t-field="doc.name" />

return stackoverflow I want remowe last 8 character and get stack

Naglis
  • 2,583
  • 1
  • 19
  • 24
user_odoo
  • 2,284
  • 34
  • 55

3 Answers3

9
<p><t t-esc="doc.name[:-8] if doc.name else ''"/></p>
Naglis
  • 2,583
  • 1
  • 19
  • 24
  • Tnx for answer work fine on char field but after put on float get error File "", line 1, in QWebException: "'float' object has no attribute '__getitem__'" while evaluating 'doc.time[:-2]' Any solution for float field? – user_odoo May 16 '17 at 09:54
  • 1
    For floats you can just convert it to string first, eg: `

    `. If you are trying to remove the digits after the decimal point, you might want to look into [string formatting](https://pyformat.info/#number).
    – Naglis May 16 '17 at 10:47
1

For string you can use this:

<p><t t-esc="doc.name[:-8] if doc.name else ''"/></p>

For float value try this one:

<p><t t-esc="'%.2f'%(t.amount)"/></p>

This will print value to exactly two decimal places

sagar soni
  • 61
  • 8
1

For "char" came from DB (just this works for me, Odoo V11)

<p><span t-if="doc.name" t-esc="str(doc.name)[:-8]"/></p>