How to Add Array in Odoo 14 from XML file

Viewed 734

I want to make an Array from XML file and put a value into it, like in the example below I want to add 5 to the Array so it become [1,2,3,5]. Can someone teach me how to do it. I tried with my code below

<t t-set="numberArray" t-value="[1,2,3]" />
<t t-set="numberArray" t-value="numberArray.append(5)" />
<t t-esc="numberArray" />

But its not working and produces an error

TypeError: scope.numberArray.append is not a function

Help me please thankss

1 Answers

Python list append function appends the value to the existing list, but does not return the new list as return value. Actually it returns None. This way your code will set the variable numberArray to value None when appending the number 5 to the list.

You can add the array either by using list + operator or by assigning the append return value to some other (dummy) variable.

Working solution with + operator:

<t t-set="numberArray" t-value="[1,2,3]" />
<t t-set="numberArray" t-value="numberArray+[5]" />
<t t-esc="numberArray" />

Working solution with dummy variable:

<t t-set="numberArray" t-value="[1,2,3]" />
<t t-set="numberArrayDummy" t-value="numberArray.append(5)" />
<t t-esc="numberArray" />

More info on Python list methods can be found here: https://docs.python.org/3/tutorial/datastructures.html. It states that

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. [1] This is a design principle for all mutable data structures in Python.

Related