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:
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
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>
- 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 modelnamemust match exactly the name of your Python functionclass="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
- Upgrade your custom module in Odoo
- Force-refresh the browser (Ctrl+Shift+R) to clear cache
- Navigate to your model’s list view—you’ll see your custom button positioned right before Create and Import
- Click it to verify your Python function executes (check the success notification in our example)
内容的提问来源于stack exchange,提问作者Carlo André Alva

