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

Odoo 16添加明细行无自动保存,如何修改one2many前端触发自动保存?

在Odoo 16中实现One2many明细行添加时自动保存的方案

为什么修改后端write方法无效

添加One2many明细行时,Odoo前端只会在客户端缓存新增记录,不会立刻调用后端write接口,只有手动点击保存或触发特定保存动作时才会同步到服务器。所以修改后端write方法无法触发自动保存逻辑。

前端实现自动保存的两种方案

方案一:全局监听所有One2many明细行添加事件

通过扩展web.FormController,监听明细行的添加操作,触发表单自动保存:

  1. 创建前端JS文件(your_module/static/src/js/auto_save_one2many.js):
odoo.define('your_module.auto_save_one2many', function (require) {
    "use strict";

    const FormController = require('web.FormController');

    FormController.include({
        _renderButtons: function () {
            this._super.apply(this, arguments);
            // 监听明细行添加按钮点击事件
            this.$el.on('click', '.o_list_button_add', () => {
                // 延迟执行,确保明细行已完成客户端初始化
                setTimeout(() => {
                    this.save()
                        .then(() => console.log('明细行添加后自动保存成功'))
                        .catch(err => console.error('自动保存失败:', err));
                }, 500);
            });
        },

        // 更精准的监听:捕获明细行记录创建完成事件
        _onRecordCreated: function (ev) {
            this._super.apply(this, arguments);
            // 判断当前创建的记录属于当前表单的One2many字段
            const parentModel = this.model.get(this.handle).model;
            if (ev.target.model === this.model.get(this.handle).data[this.currentField].relation) {
                this.save();
            }
        }
    });

    return FormController;
});
  1. 在模块__manifest__.py中注册该JS文件:
{
    'name': 'One2many自动保存',
    'version': '1.0',
    'depends': ['base', 'web'],
    'assets': {
        'web.assets_backend': [
            'your_module/static/src/js/auto_save_one2many.js',
        ],
    },
    'installable': True,
    'auto_install': False,
}

方案二:针对特定One2many字段开启自动保存

如果只需要给特定字段添加自动保存,可以通过字段属性控制:

  1. 在XML视图中给目标One2many字段添加自定义属性:
<field name="order_line" widget="one2many_list" options="{'auto_save': True}"/>
  1. 修改JS代码,仅对带有auto_save属性的字段触发自动保存:
odoo.define('your_module.auto_save_one2many', function (require) {
    "use strict";

    const FormController = require('web.FormController');

    FormController.include({
        _renderButtons: function () {
            this._super.apply(this, arguments);
            this.$el.on('click', '.o_list_button_add', (e) => {
                const $one2manyContainer = $(e.currentTarget).closest('.o_field_one2many');
                const fieldName = $one2manyContainer.data('field-name');
                const field = this.fields[fieldName];
                
                // 判断字段是否开启自动保存
                if (field.options?.auto_save) {
                    setTimeout(() => {
                        this.save();
                    }, 500);
                }
            });
        }
    });

    return FormController;
});

注意事项

  • 延迟时间(如500ms)可根据实际场景调整,确保明细行的字段初始化完成
  • 频繁自动保存会增加服务器请求,建议仅在必要场景使用
  • 如果明细行有必填字段,需确保添加时已填写完整,否则会导致保存失败,可额外添加前端校验逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 01:23:36