You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo14考勤签到签退页添加Many2one字段时QWeb遍历报错求助

问题分析与解决方案

报错原因

QWeb模板中使用t-foreach="work_status"遍历数据,但该变量并未在前端组件中定义或传递,QWeb无法找到可枚举的数据源,因此触发No enumerator given to foreach错误。

解决步骤

1. 编写前端组件(JavaScript)

继承原考勤组件,加载work.status模型的所有记录并传递给模板,同时处理签到时的字段保存逻辑:

odoo.define('hr_attendance.work_status_widget', function (require) {
    "use strict";

    const MyAttendance = require('hr_attendance.my_attendance');
    const rpc = require('web.rpc');

    MyAttendance.include({
        start: function () {
            var self = this;
            return this._super.apply(this, arguments).then(function() {
                // 获取所有工作状态记录
                rpc.query({
                    model: 'work.status',
                    method: 'search_read',
                    args: [[], ['id', 'name']],
                }).then(function(work_statuses) {
                    self.work_statuses = work_statuses;
                    self.render(); // 重新渲染模板传递数据
                });
            });
        },
        // 重写签到签退方法,保存选中的工作状态
        _attendance_action: function() {
            var self = this;
            var work_status_id = $('#locationSelected').val();
            return this._super.apply(this, arguments).then(function() {
                // 仅在签到时更新考勤记录的工作状态
                if (self.employee.attendance_state === 'checked_in') {
                    rpc.query({
                        model: 'hr.attendance',
                        method: 'search',
                        args: [[['employee_id', '=', self.employee.id], ['check_out', '=', false]]],
                        limit: 1,
                    }).then(function(attendance_ids) {
                        if (attendance_ids.length) {
                            rpc.query({
                                model: 'hr.attendance',
                                method: 'write',
                                args: [attendance_ids[0], {'work_status': parseInt(work_status_id)}],
                            });
                        }
                    });
                }
            });
        },
    });
});

2. 修正QWeb模板

将遍历变量改为组件传递的widget.work_statuses:

<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="widget.work_statuses" t-as="status">
                          <option t-att-value="status.id"><t t-esc="status.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>

3. 配置模块依赖

在模块的__manifest__.py中添加JavaScript资源:

{
    'name': 'HR Attendance Work Status',
    'version': '1.0',
    'depends': ['hr_attendance'],
    'data': [
        'views/hr_attendance_views.xml',
    ],
    'assets': {
        'web.assets_backend': [
            'your_module_name/static/src/js/work_status_widget.js',
        ],
    },
    'installable': True,
    'auto_install': False,
}

内容的提问来源于stack exchange,提问作者hardik device

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 21:25:16