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

Odoo保存并新建向导:One2many表单指定字段记忆需求

实现Odoo One2many弹窗“保存并新建”时保留指定字段值

要在one2many的表单弹窗中点击「保存并新建」后保留analytic_account_id、product_id、partner_id字段的值,可通过重写模型的create和default_get方法实现,具体代码如下:

修改mk.voucher.line模型

from odoo import models, fields, api

class MkVoucherLine(models.Model):
    _name = 'mk.voucher.line'
    voucher_id = fields.Many2one('mk.voucher', string='Voucher')
    product_id = fields.Many2one('product.product', string='Product', required=True)
    analytic_account_id = fields.Many2one('account.analytic.account', string='Analytic Account', required=True)
    partner_id = fields.Many2one('res.partner', string='Vendor', required=True,)

    @api.model
    def default_get(self, fields_list):
        res = super(MkVoucherLine, self).default_get(fields_list)
        # 从上下文中读取上次保存的字段值,设置为默认值
        ctx = self.env.context
        if 'default_analytic_account_id' in ctx:
            res['analytic_account_id'] = ctx.get('default_analytic_account_id')
        if 'default_product_id' in ctx:
            res['product_id'] = ctx.get('default_product_id')
        if 'default_partner_id' in ctx:
            res['partner_id'] = ctx.get('default_partner_id')
        return res

    def create(self, vals):
        # 创建当前记录
        record = super(MkVoucherLine, self).create(vals)
        # 将当前记录的指定字段值存入上下文,供下一条新建记录使用
        if self.env.context.get('keep_last_values'):
            ctx = self.env.context.copy()
            ctx.update({
                'default_analytic_account_id': record.analytic_account_id.id,
                'default_product_id': record.product_id.id,
                'default_partner_id': record.partner_id.id,
            })
            self = self.with_context(ctx)
        return record

调整视图按钮(可选)

如果默认的「保存并新建」按钮未自动携带keep_last_values上下文,需要在one2many弹窗的表单视图中,修改按钮的上下文参数:

<record id="mk_voucher_line_form_view" model="ir.ui.view">
    <field name="name">mk.voucher.line.form</field>
    <field name="model">mk.voucher.line</field>
    <field name="arch" type="xml">
        <form>
            <!-- 字段布局 -->
            <sheet>
                <group>
                    <field name="product_id"/>
                    <field name="analytic_account_id"/>
                    <field name="partner_id"/>
                </group>
            </sheet>
            <!-- 修改保存并新建按钮的上下文 -->
            <footer>
                <button name="save_and_new" string="保存并新建" type="object" context="{'keep_last_values': True}"/>
                <button name="save" string="保存" type="object"/>
                <button string="取消" special="cancel"/>
            </footer>
        </form>
    </field>
</record>

逻辑说明

  1. default_get方法:在加载新记录表单时,从上下文中读取之前保存的字段值,自动填充到对应字段,实现记忆效果。
  2. create方法:在创建记录后,将当前记录的指定字段ID存入上下文,确保点击「保存并新建」后,下一条记录能获取到这些值。
  3. 视图调整:通过给「保存并新建」按钮添加keep_last_values上下文参数,触发字段值记忆逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 04:02:12