
Learn how to add a Button in Form View in Odoo to trigger custom actions. Follow this guide to implement and customize Odoo Custom Buttons for better functionality.
Adding a button in form view in Odoo is a common requirement when building custom modules or enhancing existing workflows. Buttons allow users to trigger actions such as validating an order, sending an email, or generating a report directly from the form view. In this blog, we will show you how to easily add a button to an Odoo form view and bind it to a Python function to handle custom business logic.
In Odoo, a button in form view is a UI element that performs a specific action when clicked. Buttons are typically used to execute business logic or automate processes within a form, such as confirming an order or marking a task as completed. By adding buttons, you can improve user interaction and enhance the functionality of your Odoo modules.
Implementing a button in form view in Odoo offers several advantages:
Follow these steps to add a button in form view to your Odoo model and bind it to a Python function.
The first step to add a button in Odoo form view is to define the button in the form view's XML file. Here’s an example of how to add a button:
<record id="view_task_form" model="ir.ui.view">
<field name="name">project.task.form</field>
<field name="model">project.task</field>
<field name="arch" type="xml">
<form string="Task">
<header>
<button name="action_done" type="object" string="Mark as Done" class="btn-primary"/>
</header>
<sheet>
<group>
<field name="name"/>
<field name="description"/>
</group>
</sheet>
</form>
</field>
</record>
In this example, we added a button labeled "Mark as Done" inside the form’s header. The name attribute refers to the method in the Python model that will handle the action, and type="object" indicates that the button will trigger a Python method.
Once the button is defined in the form view, you need to create the corresponding Python method in your model that will be executed when the button is clicked. Below is an example:
from odoo import models, fields
class Task(models.Model):
_inherit = 'project.task'
def action_done(self):
self.write({'state': 'done'})
In this example, the action_done method sets the task's state to "done" when the button is clicked. You can customize this method to execute any business logic, such as sending an email, generating a report, or validating a record.
To ensure that only authorized users can access the button in form view, you may need to add security rules. You can do this by defining record rules or access control lists (ACLs) that restrict access to the button’s functionality. This ensures that only users with the proper permissions can trigger the button’s action.
Odoo allows you to customize the appearance and behavior of buttons. Here are some common attributes you can use when adding a button in form view in Odoo:
btn-primary
, btn-success
, or btn-danger
to style the button.type="object"
for Python methods or type="action"
for server actions.confirm
attribute to display a confirmation dialog before executing the action, e.g., confirm="Are you sure?"
.To ensure the best user experience and avoid potential issues, follow these best practices when adding a button in form view in Odoo:
Adding a button in form view in Odoo is a straightforward process that enhances user interaction and enables the execution of custom workflows directly from the form interface. By following this guide, you can easily implement buttons to trigger specific actions or business logic, improving both user experience and workflow efficiency in your Odoo environment.
Your email address will not be published. Required fields are marked *