Search code examples
python-3.xodoo

Py3o.template : How to increment py3o variable inside a loop?



I'm working on .odt report using py3o.template in Odoo.
In the report, I need to add a number in a Group, so if I have 3 groups, It should print out :
Group 1
Group 2
Group 3
I did it like in this image :
enter image description here
It doesn't work and I get these errors :

"Could not move siblings for '%s'" % py3o_base
py3o.template.main.TemplateException: Could not move siblings for 'with="i=1"'

Can you help me?
PS : I need to call "i" between Group and [date_start]


Solution

  • This error happens when you did not have close properly your tags.

    I can see only one closing with, try to close the two others.

    Check the following example:

    with="index=1"
        with="index=index+1"
            with="index=index+1"
                function="index"  index=3
            /with
            function="index" index=2
        /with
        function="index" index=1
    /with
    
    function="index" index undefined
    

    Note that if a variable of the same name already existed outside of the scope of the with directive, it will not be overwritten. Instead, it will have the same value it had prior to the with assignment. Effectively, this means that variables are immutable.

    To avoid that we can use a list to hold indexes and in each iteration we will create a new variable to hold the new index (we can use list.pop(0) to remove the previous index).

    with="index=[1]"
      for="item in range(4)"
        function="index[-1]"
    
        with="_=index.append(index[-1]+1)"
        /with
    
        with="_=index.pop(0)"
        /with
    
      /for
    
    At the end index = function="str(index)"
    /with
    

    The output should be:

    1
    2
    3
    4
    At the end index == [5]
    

    To enumerate the loop items I highly recommend using enumerate function.

    for="index,d in enumerate(o.get_session_date_ids(), 1)"
    function="index"