DEV Community

Cover image for Odoo: Add a new wizard
Jeevachaithanyan Sivanandan
Jeevachaithanyan Sivanandan

Posted on

Odoo: Add a new wizard

In Odoo development, it's common to create message popups that assist users in selecting options. These popups, known as Wizards in Odoo, can include choices like "ignore and continue" or "cancel". They are typically triggered based on certain condition checks.

Let's explore how to create such a popup. Specifically, we'll focus on creating a popup that activates when a user clicks on the "VALIDATE" button during manufacturing order transfer.

Let's generate a new .py file within the "wizard" folder of the custom module. Following this naming convention in Odoo ensures that any code associated with wizards is organized within the designated folder, thereby enhancing readability.

custom_module\linco_care\wizard\tolerance_warning_wizard.py

For this wizard model, we will incorporate two buttons. Consequently, we will establish new methods for each button as outlined below.

from odoo import api, fields, models

class StockWarningWizard(models.TransientModel):
    _name = 'stock.warning.wizard'
    _description = 'Stock Warning Wizard'

    message = fields.Text(string="Message", readonly=True)

    def ignore_continue(self):
        return self.env['stock.picking'].ignore_continue()

    def go_back(self):
        return True
Enter fullscreen mode Exit fullscreen mode

Consecutively, we shall create .xml view file for the same wizard in the location - custom_module\wizard\tolerance_warning.xml

<odoo>
    <record id="view_stock_warning_wizard" model="ir.ui.view">
        <field name="name">stock.warning.wizard</field>
        <field name="model">stock.warning.wizard</field>
        <field name="arch" type="xml">
            <form string="Acceptable Tolerance Warning">
                <group>
                    <div colspan="2">
                        <p name="explanation-text">
                            The quantity picked for at least one of the components is out of the acceptable tolerance.
                        </p>
                    </div>
                </group>
                <footer>
                    <button string="Ignore and  Continue" type="object" name="ignore_continue" class="btn-primary"/>
                    <button string="Go Back" type="object" name="go_back" class="btn-secondary"/>
                </footer>
            </form>
        </field>
    </record>
                <record id="action_stock_warning_wizard" model="ir.actions.act_window">
                <field name="name">Acceptable Tolerance Warning</field>
                <field name="res_model">stock.warning.wizard</field>
                <field name="view_mode">form</field>
                <field name="target">new</field>
                <field name="view_id" ref="view_stock_warning_wizard"/>
            </record>
</odoo>

Enter fullscreen mode Exit fullscreen mode

Now, we must activate this wizard when the user clicks the VALIDATE button on the manufacturing order page. To achieve this, we inherit the stock.picking model in our custom module and then inherit the existing button_validate method within our model, as illustrated below:

from odoo import  _, api, fields, models

class Picking(models.Model):
    _inherit = "stock.picking"

    def button_validate(self):
        action = self.env.ref('linco_care.action_stock_warning_wizard').read()[0]
        action['context'] = {
                'default_picking_id': self.id,
            }
        return action

    def ignore_continue(self):
        return self.env['stock.picking'].button_validate()
Enter fullscreen mode Exit fullscreen mode

this is how we can create a new wizard in the Odoo

note : This has been tested in Odoo version 17

Top comments (0)