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

Odoo V11中如何在Create和Import按钮前添加可调用Python函数的按钮

Hey Carlo, I’ve dealt with this exact version-specific quirk in Odoo before—those older solutions fail because V11 changed how list view action buttons are structured. Let’s get your custom button placed before Create/Import and linked to a Python function with this step-by-step fix:

Step 1: Add Your Custom Python Function to the Model

First, extend the target model and define the function you want the button to trigger. Use @api.model since this action will run from the list view (not tied to a specific record):

from odoo import models, api

class YourTargetModel(models.Model):
    _inherit = 'your.target.model'  # Replace with the actual model (e.g., 'res.partner')

    @api.model
    def trigger_custom_action(self):
        # Add your custom logic here
        # Example: Log a message, create a record, or send a notification
        self.env.user.notify_success(message="Custom button action ran successfully!")
        return True
Step 2: Inherit the Tree View to Insert the Button

In Odoo V11, list view action buttons live inside the <header> tag (unlike older versions where you could inject them directly into the tree). We’ll inherit the base tree view and position our button before the default Create/Import buttons:

<odoo>
    <record id="inherit_your_model_tree" model="ir.ui.view">
        <field name="name">your.target.model.tree.custom.button</field>
        <field name="model">your.target.model</field>
        <field name="inherit_id" ref="base.view_res_partner_tree"/> <!-- Replace with your base tree view ID -->
        <field name="arch" type="xml">
            <!-- Insert button inside the header, before existing actions -->
            <header position="inside">
                <button 
                    name="trigger_custom_action" 
                    string="My Custom Button" 
                    type="object" 
                    class="oe_highlight"
                    position="before"
                />
            </header>
        </field>
    </record>
</odoo>
Critical V11-Specific Notes
  • Why old solutions didn’t work: V8-V10 allowed placing buttons directly in the <tree> node, but V11 moved all list view action buttons to the <header> section for consistency.
  • Button attributes:
    • type="object" tells Odoo to call a Python method on the model
    • name must match exactly the name of your Python function
    • class="oe_highlight" gives it the same styled look as Create/Import (optional but recommended)
    • Add groups="base.group_manager" if you want to restrict the button to specific user groups
Test the Fix
  1. Upgrade your custom module in Odoo
  2. Force-refresh the browser (Ctrl+Shift+R) to clear cache
  3. Navigate to your model’s list view—you’ll see your custom button positioned right before Create and Import
  4. Click it to verify your Python function executes (check the success notification in our example)

内容的提问来源于stack exchange,提问作者Carlo André Alva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 06:53:45