odoo-11qweb

Odoo 11: Pass array to qweb template


I'm trying to pass an array from my controller to a QWeb template but can't achive to iterate through it...

--- Controller ---

return_projects = {}

for active_project in active_projects:
    hours = 0
    for analytic_line in active_project.line_ids:
        hours += analytic_line.unit_amount

    return_projects[active_project.id] = {
        "name": active_project.display_name,
        "hours": hours
    }

return http.request.render('mymodule_briefing.crm_page', {
    'projects': return_projects,
})

--- Template ---

<t t-foreach="projects" t-as="project">
<-- show project.name -->
<-- show project.hours-->
</t>

Solution

  • Inside the loop, project is the project id so you need to use it to get project data (name and hours) from projects

    Example:

    <t t-foreach="projects" t-as="project_id">
        <div>
            <t t-esc="projects[project_id]['name']"/>
            <t t-esc="projects[project_id]['hours']"/>
        </div>
    </t>  
    

    You can render the template using the project records:

    return odoo.http.request.render('mymodule_briefing.crm_page', {
        'projects': active_projects,
    })  
    

    And use the following code to loop over records and compute hours:

    <t t-foreach="projects" t-as="project">
        <div>
            <t t-esc="project.name"/>
            <t t-esc="sum(line.unit_amount for line in project.line_ids)"/>
        </div>
    </t>