odoo / owl

OWL: A web framework for structured, dynamic and maintainable applications
https://odoo.github.io/owl/
Other
1.14k stars 344 forks source link

Change in how t-set works for booleans between Odoo 14 and 16 #1552

Closed mavaa closed 11 months ago

mavaa commented 11 months ago

In Odoo 14, I had the following xml:

<t t-set="firstItem" t-value="True"/>

<t t-foreach="i.package_level_ids" t-as="o">
    [...]

    <t t-if="not firstItem">
        <i class="fa fa-fw fa-circle"
            style="font-size: 1px; vertical-align: middle; line-height: 1em; margin: 0 4px 0 3px;"/>
    </t>
    <t t-set="firstItem" t-value="False"/>

</t>

This way, firstItem was set to the boolean value True, then False, so I could do special styling for the first item in my list. However, after upgrading to Odoo 16, we get the following error:

Error while render the template
TypeError: '<' not supported between instances of 'bool' and 'str'
Template: sale.sale_order_portal_content
Path: /t/div[2]/div[1]/t[6]/div/div[2]/t/t[3]
Node: <t t-set="firstItem" t-value="True"/>

Seems that in Odoo 16, the value is set to the string "True", not the boolean (python) built in True. Is this a bug, or is there a way I can get the old behaviour back?

ged-odoo commented 11 months ago

Hello!

This seems to be an issue with qweb templates rendered on the server. This repository is for Owl, which is the framework that renders template in the browser (so, not the same code!). @Gorash may know more about the problem.

Now, in your case, i would suggest to use a specific qweb feature: if you iterate in a t-foreach loop, you get some additional useful variables, such as o_first, which is true if this is the first iteration. This means that your code can probably be written like this:

<t t-foreach="i.package_level_ids" t-as="o">
    [...]

    <t t-if="not o_first">
        <i class="fa fa-fw fa-circle"
            style="font-size: 1px; vertical-align: middle; line-height: 1em; margin: 0 4px 0 3px;"/>
    </t>
</t>

https://www.odoo.com/documentation/master/developer/reference/frontend/qweb.html#loops

mavaa commented 11 months ago

That's great, thanks!

And also, I might be mistaken about the setting of the variable to True, when I removed the code, I got the exact same error, but from a different line, so it might look like Odoo isn't reporting the correct offending line here.. Will report back and possibly close as soon as I figure out what is actually happening here.

mavaa commented 11 months ago

Sorry, I was mistaken in what the original issue was, after removing almost my entire template, it turned out to be an issue in a completely separate part of my code.