What is an Odoo Wizard?
A wizard in Odoo is a popup window primarily used to perform user-defined actions. It operates using a Transient Model, which means the data it handles is stored temporarily and is automatically deleted from the database at regular intervals.
Steps to Create an Odoo Wizard Model
- Create the Model
Define the wizard model with the necessary fields in a .py file.
python
from odoo import models, fields, api
class SaleOrderDiscountWizard(models.TransientModel):
_name = ‘sale.order.discount.wizard’
_description = ‘Apply Discount to Sale Orders’
discount = fields.Float(string=’Discount (%)’, required=True)
sale_order_ids = fields.Many2many(
‘sale.order’,
string=’Sale Orders’,
required=True
)
def apply_discount(self):
for order in self.sale_order_ids:
for line in order.order_line:
line.discount = self.discount
Calling the Wizard from a Menu Item
To trigger a wizard from a menu item, the action parameter of the menu should reference the view ID of the wizard.
- Define the Wizard View
Create an .xml file to define the wizard’s view.
xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!– Define the wizard form view –>
<record id="sale_order_discount_wizard_view_form" model="ir.ui.view">
<field name="name">sale.order.discount.wizard.form</field>
<field name="model">sale.order.discount.wizard</field>
<field name="arch" type="xml">
<form string="Apply Discount">
<group>
<field name="discount"/>
<field name="sale_order_ids" widget="many2many_tags"/>
</group>
<footer>
<button string="Apply" type="object" name="apply_discount" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
<!– Define the wizard action –>
<record id="sale_order_discount_wizard_action" model="ir.actions.act_window">
<field name="name">Apply Discount</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">sale.order.discount.wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="sale_order_discount_wizard_view_form"/>
<field name="target">new</field> <!– Opens in a popup –>
</record>
<!– Add the menu item to call the wizard –>
<menuitem id="sale_order_discount_wizard_menu_action" sequence="10" name="Apply Discount"
parent="sale.sale_order_menu" action="sale_order_discount_wizard_action"/>
</odoo>
Integrate the Wizard into the Module
- Update __init__.py
Include the .py file in the __init__.py of the models folder.
python
from . import sale_order_discount_wizard
- Update __manifest__.py
Add the .xml file to the data section of the module’s manifest.
python
‘data’: [
‘views/sale_order_discount_wizard.xml’,
],
Apply Changes
- Restart the Odoo server.
- Upgrade the custom module.
The wizard will now be accessible through the menu and will function as defined.