
Learn how to launch a wizard from a button in Odoo with step-by-step examples using Python and XML. Simplify workflows with Odoo wizards.
In Odoo, wizards are a powerful feature that allows developers to create multi-step processes or forms for users. One of the most common use cases for wizards is triggering them through a button click, which simplifies workflows and improves user interaction. This blog will guide you through how to launch a wizard from a button in Odoo with step-by-step instructions, including examples of XML and Python code.
A wizard in Odoo is essentially a pop-up window (modal) that helps users input or modify data by walking them through a process. Wizards are often used to guide users through complex actions that require multiple steps or inputs. A wizard can be opened using a button in Odoo views, making it easy to interact with the form and perform actions.
Launching a wizard from a button click in Odoo can enhance the user experience by providing a structured way to gather data or perform specific actions without navigating through multiple menus. It streamlines operations such as creating orders, confirming actions, or configuring settings through a user-friendly interface.
Follow these steps to create and launch a wizard from a button in Odoo:
The first step in creating a wizard is to define the model. Wizards are typically transient models, meaning the data they handle is temporary and doesn’t persist in the database. Here’s an example of a simple wizard model:
from odoo import models, fields, api
class MyWizard(models.TransientModel):
_name = 'my.wizard'
_description = 'My Custom Wizard'
input_data = fields.Char(string="Input Data")
def action_confirm(self):
# Code to perform some action when the user confirms
print("Wizard Confirmed with Input:", self.input_data)
In this example, we define a transient model called my.wizard with a simple input field and a method named action_confirm that will be triggered when the wizard is confirmed.
To launch the wizard, we need to create a button in the XML view that will trigger the wizard. Here’s how you can add the button:
<record id="view_form_model" model="ir.ui.view">
<field name="name">form.view</field>
<field name="model">your.model</field>
<field name="arch" type="xml">
<form>
<group>
<button name="action_open_wizard" type="object" string="Launch Wizard" class="btn-primary"/>
</group>
</form>
</field>
</record>
In this example, a button labeled "Launch Wizard" is created. When clicked, it will trigger the method action_open_wizard in the corresponding model.
In the backend, we need to define the method that opens the wizard when the button is clicked:
from odoo import models, api
class YourModel(models.Model):
_name = 'your.model'
@api.multi
def action_open_wizard(self):
return {
'type': 'ir.actions.act_window',
'name': 'My Wizard',
'res_model': 'my.wizard',
'view_mode': 'form',
'target': 'new',
}
The action_open_wizard method triggers the wizard by returning an ir.actions.act_window action. This opens the wizard in a pop-up (modal) window, where users can input data or make choices.
Now, we need to define the form view for the wizard in XML. This form will appear when the wizard is triggered by the button:
<record id="view_my_wizard_form" model="ir.ui.view">
<field name="name">my.wizard.form</field>
<field name="model">my.wizard</field>
<field name="arch" type="xml">
<form string="Wizard Form">
<group>
<field name="input_data"/>
</group>
<footer>
<button string="Confirm" type="object" name="action_confirm" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
This defines the form that will be displayed inside the wizard. It includes an input field for the user to provide data, and buttons for confirming or canceling the wizard.
Launching a wizard from a button in Odoo offers several key benefits:
Launching a wizard from a button in Odoo is a great way to streamline workflows and guide users through specific actions. By following the steps outlined in this guide, you can easily create and launch wizards in your Odoo applications, improving both functionality and user experience. For more tips on Odoo customization, check out the Odoo official documentation or explore Odoo blogs for advanced customization options.
Your email address will not be published. Required fields are marked *