Odoo 17 Development Book- Wizard

Wizard

Odoo wizards are used to create user-interactive sections. Such interactive sections are extremely beneficial to the flow of any business. In Odoo, the most commonly used model classes for wizard creation are transient and abstract models. The most common mode, however, is the transient mode. The data in a Transient model is deleted on a regular basis, hence the name Transient. Database persistent model operations are frequently performed using wizards.

Consider an example of a use case. Consider a good example of a student.student in charge of all student records in an educational organisation

from odoo import fields, models, api


class Student(models.Model):
    _name = "student.student"
    _description = "Student"

    name = fields.Char(string="Name", required=True)
    partner_id = fields.Many2one('res.partner', string="Partner")
    phone = fields.Char(string="Phone Number")
    email = fields.Char(string="Email", required=True)
    status = fields.Char(string="Status")
    leave_ids = fields.One2many('student.leave', 'student_id', string="Leaves")

There is a one2many type field for recording all of the student's leaves taken. Let's make the model student.leave the required fields.

class StudentLeave(models.Model):
    _name = "student.leave"
    _description = "Student Leave"

    student_id = fields.Many2one('student.student', string="Student")
    date = fields.Date(string="Date")
    reason = fields.Char(string="Reason")

The wizard model will then be created, allowing the user to choose a date and provide a reason for the leave. The transient model class is used for this wizard model.

class StudentLeaveWizard(models.TransientModel):
    _name = 'student.leave.wizard'
    _description = 'Student Leave'

    student_id = fields.Many2one('student.student', string="Student",
                                readonly=True)
    date = fields.Date(string="Date")
    reason = fields.Char(string="Reason")

Inside the student form view, add a button for creating a leave record.

<header>
    <button name="create_leave" string="Leave" class="oe_highlight"
            type="object"/>
</header>

Create a Python function for the button action in the student model. Let us return a widget where the user can enter the departure date and reason. Using the widget values, create a new leave record for the student. In the widget model, add a new record with the student_id field set to the current student. Then, when you click the button, return that object.

def create_leave(self):
    wizard = self.env['student.leave.wizard'].create({
       'student_id': self.id
    })
    return {
       'name': _('Student Leave'),
       'type': 'ir.actions.act_window',
       'res_model': 'student.leave.wizard',
       'view_mode': 'form',
       'res_id': wizard.id,
       'target': 'new'
    }

The next step is to design a form view for the wizard that includes buttons for performing actions on the record. Two buttons named CREATE and CANCEL are added to the wizard view, each with the functionality indicated by the name.

<record model="ir.ui.view" id="leave_wizard_form_view">
    <field name="name">Leave</field>
    <field name="model">student.leave.wizard</field>
    <field name="arch" type="xml">
        <form string="Leave">
            <sheet>
                <group>
                    <group>
                        <field name="student_id"/>
                    </group>
                    <group>
                        <field name="date" required="1"/>
                    </group>
                </group>
                <separator string="Reason"/>
                <field name="reason" required="1" nolabel="1" placeholder="Give leave reason"/>
            </sheet>
            <footer>
                <button type="object" name="create_leave_from_wizard" class="btn btn-primary"
                        string="CREATE"/>
                <button string="CANCEL" special="cancel"/>
            </footer>
        </form>
    </field>
</record>

This will return the widget form when the button is clicked.

odoo-development

The required action will now be performed within the create_leave_from_wizard method. This method must be written within the wizard model. Create a new record in the model student.leave using the widget value on the button click.

def create_leave_from_wizard(self):
   self.env['student.leave'].create({
       'student_id': self.student_id.id,
       'date': self.date,
       'reason': self.reason
   })

After making all necessary changes, upgrade the module and verify that the student's new leave record was successfully created.

In Odoo 17 we have the option to drag the wizard. Just click on the wizard and drag it anywhere you want.

This is how a wizard is created, which improves the overall user experience.

whatsapp
location

Calicut

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

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