Enable Dark Mode!
how-to-create-manage-wizard-in-the-odoo-15.jpg
By: Aswani VT

How to Create & Manage Wizard in the Odoo 15

Technical Odoo 15

Wizards in Odoo are used for creating interactive sessions to enhance the user experience. For any business, such interactive sessions are very beneficial. Transient and Abstract models have mainly utilized model classes in odoo for creating wizards and the Transient model is the most commonly used one. This gives all the features of a model class except that the data in a Transient model is stored temporarily and is deleted periodically, hence the name Transient. Wizards are commonly used to perform operations on permanent models.
The TransientModel class extends the Model class and uses its existing particularities. Following are some of them:
a) Wizard data are not permanent; they are automatically removed from the database after some time. 
b) Users have all access to wizard data.
c) Wizard records may allude to regular data or wizard data through many2one fields, but regular data cannot allude to wizard data through a many2one field.
Now we can see  how  a wizard is created using the transient model class,
Class TestModelWizard(models.TransientModel):
_name =  ‘test.model.wizard’
_description = ‘Test Model Wizard’
test_field = fields.Char(string = ‘Test Field’)
Wizards are opened as ir.actions.act_window data, with the field target given the value new. It opens the wizard view into a popup window. The wizard action may be triggered using a menu item or a button action either in python code or XML code.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="test_model_wizard_view_form" model="ir.ui.view">
        <field name="name">test.model.wizard.form</field>
        <field name="model">test.model.wizard</field>
        <field name="arch" type="xml">
            <form string="Test">
                <group class="oe_title">
                    <field name="test_field"/>
                </group>
                            </form>
        </field>
    </record>
    <record id="test_model_wizard_action" model="ir.actions.act_window">
        <field name="name">Test Model Wizard</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">test.model.wizard</field>
        <field name="view_mode">form</field>
        <field name="view_id" ref="test_model_wizard_view_form"/>
        <field name="target">new</field>
    </record>
</odoo>
Wizards use the attribute special="cancel" to close the wizard window without saving. Let’s see how
<footer>
    <button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
Now we can see how we can add a button and action to that button in a wizard,
def action_done(self):
records = self.env['test.model'].browse(self.env.context.get('active_ids'))
for rec in records: 
rec.write({'state' : ’done’})
The above method will change the status of all active records in the model ‘test.model’ into the ‘done’ state.
Now we can add the action into the wizard's view,
 <footer>
<button name="action_done" string="Submit" type="object" class="btn-primary"/>
</footer>
Next is how we can open a wizard either using a button or menu item
The first is how to open a wizard from a button click. For this, we can  use either of the two methods one using python code or another using XML code as below,
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="test_model_view_form" model="ir.ui.view">
        <field name="name">test.model.form</field>
        <field name="model">test.model</field>
        <field name="arch" type="xml">
            <form string="Test">
<header >
   <button name="create_wizard" string="Create Wizard" class="oe_highlight" type="object"/>
</header>                    </form>
        </field>
    </record>
Now we can write the return action for creating wizard in  ‘ test.model’,
def create_wizard(self):
   wizard = self.env['test.model.wizard'].create({
    'test_field': self.name
   })
   return {
    'name': _('Test Wizard'),
    'type': 'ir.actions.act_window',
    'res_model': 'test.model.wizard’,
    'view_mode': 'form',
    'res_id': wizard.id,
    'target': 'new'
   }
Another method is using  XML code  as below,
<button name="%(module_name..test_model_wizard_action)d" title="Create Wizard" type="action"/>
Likewise, we can add the wizard action to a men items action and launch wizard using a menuitem as below,
<menuitem id="test_model_wizard_menu_action" 
sequence="10"
 name="Wizard" action="test_model_wizard_action"/>
After performing all the changes, upgrade the module and check. This is how a wizard is created in odoo 15 which will enhance the overall user experience.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 
whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message