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

如何在Odoo 12的product_template看板视图头部添加按钮与字段?

排查Odoo 12产品模板看板视图头部按钮/筛选失效问题

我来帮你逐一排查两段代码的问题,再给出可行的解决办法:

第一段QWeb模板代码的问题与修正

你的第一段代码存在两个关键问题:

  1. HTML语法错误:按钮标签写岔了——<button> class="..."多了一个闭合的>,导致浏览器无法正确解析这个按钮元素。
  2. 模板加载遗漏:如果这个XML文件没被添加到模块的__manifest__.py的qweb列表里,Odoo根本不会加载这个模板。

修正后的QWeb代码

<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
    <t t-extend="KanbanView.buttons">
        <t t-jquery="button.oe_kanban_add" t-operation="after">
            <t t-if="widget.model === 'product.template'">
                <button class="oe_button oe_highlight" type="button">My Button</button>
            </t>
        </t>
    </t>
</templates>

额外配置

记得把模板文件路径添加到模块的__manifest__.py中:

'qweb': [
    'static/src/xml/your_kanban_buttons.xml',  # 替换成你的实际文件路径
],

第二段视图继承代码的问题与修正

这段代码的核心问题是对Odoo看板视图结构不熟悉:Odoo的看板视图根节点是<kanban>,它并没有<header>节点,原生看板的头部按钮/筛选控件是放在<kanban>下的<buttons>节点里的。你手动添加的<header>不会被Odoo识别,所以按钮自然不显示。

修正后的视图继承代码(支持按钮+筛选字段)

<odoo>
    <record id="product_template_kanban_inherit_buttons" model="ir.ui.view">
        <field name="name">product.template.kanban.inherit.buttons</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_kanban_view"/>
        <field name="arch" type="xml">
            <!-- 定位到看板根节点,在内部添加buttons区域 -->
            <xpath expr="//kanban" position="inside">
                <buttons>
                    <!-- 自定义按钮 -->
                    <button class="oe_button oe_highlight" type="button">My Button</button>
                    <!-- 添加筛选字段(示例:按产品分类筛选) -->
                    <field name="categ_id" widget="many2one" options="{'no_open': True}" placeholder="Filter by Category"/>
                </buttons>
            </xpath>
        </field>
    </record>
</odoo>

通用注意事项

  • 修改完代码后,必须升级你的模块才能让变更生效;
  • 确认product.product_template_kanban_view是正确的原生视图引用(Odoo 12中这个ID是对的);
  • 如果添加筛选字段,要确保字段属于product.template模型,并且当前用户有该字段的访问权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:37:55