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

Odoo销售订单行添加弹窗按钮调用自定义表单的实现指引

在Odoo前端实现销售订单行弹窗关联表单的操作指南

一、给销售订单行添加操作按钮

  1. 进入设置->技术->视图->视图,搜索并打开sale.order.line的树视图(通常外部ID为sale.order.line.tree)进行编辑。
  2. 在<tree>标签内的合适位置插入按钮代码,示例:
<button name="open_technical_params" string="技术参数" type="action" icon="fa-cog"/>

二、创建服务器动作实现弹窗逻辑

  1. 进入设置->技术->动作->服务器动作,点击创建:
    • 名称:打开技术参数表单
    • 模型:sale.order.line
    • 动作类型:Python代码
    • 在Python代码框中粘贴以下内容(注意替换模块名):
# 获取当前选中的销售订单行
current_line = self.env['sale.order.line'].browse(env.context.get('active_id'))
# 获取关联的参数记录
params_records = current_line.x_sale_order_lines_params_ids

# 构建弹窗上下文,自动关联当前订单行
popup_context = {
    'default_x_sale_order_line_id': current_line.id,
    'form_view_ref': '你的模块名.sale.order.lines.param.view.form'  # 替换为你的表单视图外部ID
}

# 根据是否有记录,打开对应表单
if params_records:
    return {
        'name': '技术参数',
        'type': 'ir.actions.act_window',
        'res_model': 'x_sale.order.line.params',
        'res_id': params_records.id,
        'view_mode': 'form',
        'view_type': 'form',
        'context': popup_context,
        'target': 'new'  # 弹窗模式
    }
else:
    return {
        'name': '技术参数',
        'type': 'ir.actions.act_window',
        'res_model': 'x_sale.order.line.params',
        'view_mode': 'form',
        'view_type': 'form',
        'context': popup_context,
        'target': 'new'
    }
  1. 保存后,复制该服务器动作的外部标识(表单顶部字段)。

三、关联按钮与服务器动作

回到编辑的sale.order.line树视图,将按钮的name值替换为刚才复制的服务器动作外部ID,保存视图修改。

四、表单视图优化(可选)

你现有的表单视图嵌套了tree组件,可调整为更适配弹窗的结构,示例:

<form string="技术参数">
    <group>
        <field name="x_sale_order_line_id" invisible="1"/>
        <page string="参数列表">
            <field name="x_sale_order_lines_params_ids" widget="one2many_list" editable="bottom">
                <tree>
                    <field name="x_param_name"/>
                    <field name="x_param_value"/>
                    <field name="x_param_uom"/>
                </tree>
            </field>
        </page>
    </group>
    <footer>
        <button string="保存" type="object" class="btn-primary"/>
        <button string="取消" special="cancel" class="btn-secondary"/>
    </footer>
</form>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 16:01:22