Search code examples
pythonodooodoo-14qweb

Error: QWeb2 - template['HrAttendanceMyMainMenu']: No enumerator given to foreach in odoo 14


I wanted to add a additional field (Many2one) on the CHECK-IN-CHECK-OUT screen.

Here's my model:

class HrAttendance(models.Model):
    _inherit = "hr.attendance"

    work_status = fields.Many2one('work.status', string='Work Status')

Here's my XM view:

 <t t-name="HrAttendanceMyMainMenu">
        <div class="o_hr_attendance_kiosk_mode_container o_home_menu_background">
            <span class="o_hr_attendance_kiosk_backdrop"/>
            <div class="o_hr_attendance_clock text-center"/>
            <div class="o_hr_attendance_kiosk_mode">
                <t t-set="checked_in" t-value="widget.employee.attendance_state=='checked_in'"/>
                <t t-if="widget.employee">
                    <div class="o_hr_attendance_user_badge o_home_menu_background">
                        <img class="img rounded-circle" t-attf-src="/web/image?model=hr.employee.public&amp;field=image_128&amp;id=#{widget.employee.id}" t-att-title="widget.employee.name" t-att-alt="widget.employee.name"/>
                    </div>
                    <div class="form-group">
                        <select class="form-control" id="locationSelected">
                          <t t-foreach="work_status" t-as="location">
                              <option t-att-value="location.id"><t t-esc="location.name"/></option>
                          </t>
                        </select>
                    </div>
                    <h1 class="mb8"><t t-esc="widget.employee.name"/></h1>
                    <h3 class="mt8 mb24"><t t-if="!checked_in">Welcome!</t><t t-else="">Want to check out?</t></h3>
                    <h4 class="mt0 mb0 text-muted" t-if="checked_in">Today's work hours: <span t-esc="widget.hours_today"/></h4>
                    <a class="fa fa-7x o_hr_attendance_sign_in_out_icon fa-sign-out btn-warning" t-if="checked_in" aria-label="Sign out" title="Sign out"/>
                    <a class="fa fa-7x o_hr_attendance_sign_in_out_icon fa-sign-in btn-secondary" t-if="!checked_in" aria-label="Sign in" title="Sign in"/>
                    <h3 class="mt0 mb0 text-muted">Click to <b t-if="checked_in">check out</b><b t-if="!checked_in">check in</b></h3>
                </t>
                <t t-else="">
                    Warning : Your user should be linked to an employee to use attendance. Please contact your administrator.
                </t>
            </div>
        </div>
    </t>

shows following traceback:

Traceback:
Error: QWeb2 - template['HrAttendanceMyMainMenu']: No enumerator given to foreach
    at Object.exception (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4408:7)
    at Object.foreach (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4422:80)
    at Engine.eval (eval at _render (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4450:73), <anonymous>:26:24)
    at Engine._render (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4449:296)
    at Engine.render (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4449:151)
    at Engine._render (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4453:57)
    at Engine.render (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4449:151)
    at Class.renderElement (http://localhost:8069/web/content/790-9a5b8a1/web.assets_backend.js:488:452)
    at prototype.<computed> [as renderElement] (http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4651:488)
    at http://localhost:8069/web/content/684-8b6b22b/web.assets_common.js:4996:6

I have already created values for work_status field but unable to figure out how to rendeer values in dropdown.

Can anyone pls help with that, TIA


Solution

  • The work_status variable is not defined. You can check the source code of the qweb2 foreach directive:

    if (enu != null) {
        ...
     } else {
        this.exception("No enumerator given to foreach", context);
     } 
    

    Available variable names like employee or hours_today are defined in MyAttendances action, you can override the same function to define the work_status values and use them in the template

    Example:

    odoo.define('stackoverflow_14.my_attendances', function (require) {
    "use strict";
    
    var MyAttendances = require('hr_attendance.my_attendances');
    
    MyAttendances.include({
        willStart: function () {
            var self = this;
            var def = this._rpc({
                    model: 'work.status',
                    method: 'search_read',
                    args: [[], ['name']],
            })
           .then(function (res) {
               self.work_status = res;
           });
           return Promise.all([def, this._super.apply(this, arguments)]);
       },
    });
    });