如何让Odoo向导中Tree视图的按钮可点击?
解决Odoo向导Tree视图按钮无法触发Python方法的问题
在Odoo向导的Tree视图中添加了按钮,点击后对应的Python方法始终未被调用。已给向导模型和向导行模型设置了1,1,1,1的访问权限,且为适配多字段需求加宽了向导窗口。以下是相关代码,求解决方法。
简化后的表单视图代码
<record model="ir.ui.view" id="vendor_connector_purchase_wizard_form"> <field name="name">vendor.connector.purchase.wizard.form</field> <field name="model">vendor.connector.purchase.wizard</field> <field name="arch" type="xml"> <form string="Vendor Connector Purchase Helper"> <script> $(document).ready(function(){ $('.modal-dialog').css({'max-width': '50%'}); $('.modal-content').css({'max-width': '100%'}); }); </script> <group string="Purchase Order Helper Lines"> <field name="purchase_order_state" invisible="1" readonly="1"/> <field name="purchase_order_helper_line_ids" mode="tree" nolabel="1" style="width:105%;"> <tree string="Purchase Order Lines" create="0" delete="0" editable="bottom"> <!-- Other fields removed for simplification --> <field name="price_subtotal" widget="monetary" string="Subtotal"/> <!-- BUTTON IS HERE --> <button name="button_use_candidate" type="object" string="Use Candidate" confirm="Are you sure?" class="btn-success"/> </tree> </field> </group> <!-- Second group was removed for simplification --> <footer> <button name="action_done" string="Done" type="object" class="btn-primary" confirm="Are you Sure?"/> <button string="Cancel" special="cancel" class="btn-secondary" /> </footer> </form> </field> </record>
按钮方法代码
def button_use_candidate(self): print("button test")
向导调用代码
def action_vendor_connector_purchase_wizard(self): self.ensure_one() action = self.env["ir.actions.actions"]._for_xml_id( "vendor_eshop_connector.vendor_connector_purchase_wizard_wizard_action" ) action["context"] = dict(self.env.context) action["context"].update( active_id=self.id, active_ids=self.ids, ) return action
问题排查与解决方法
- 确认方法所属模型:Tree视图绑定的是向导行模型(
vendor.connector.purchase.wizard.line),按钮button_use_candidate必须定义在向导行模型中,而非主向导模型。如果当前方法写在主向导模型里,点击按钮时Odoo会找不到对应方法。 - 检查按钮属性:确保按钮的
type="object"属性正确,不要写成type="action"或其他值。 - 验证权限配置:确认向导行模型的权限记录正确关联到对应模型,且当前用户所属权限组包含该权限。
- 刷新系统缓存:修改代码后清除Odoo缓存并重新升级模块,确保视图和方法的改动被系统加载。
- 查看浏览器报错:打开浏览器开发者工具(F12),检查Console标签页是否有JS报错,比如按钮绑定失效、模型找不到等信息,快速定位问题。
内容的提问来源于stack exchange,提问作者xixo222
相关产品推荐
相关产品推荐

