In this tutorial we will learn about the wizard in odoo.
"wizard" is essentially a transient model (a model whose data doesn't persist in the database like regular models) used to assist users through a series of steps to accomplish a task. Wizards are used to guide the user through multi-step processes, often providing a UI with forms and buttons for user input. Here's a detailed explanation on how to create and implement a wizard in Odoo:
Steps to Create a Wizard in Odoo
1.Define Wizard Model
This is usually done in a Python file in your custom module. The model inherits from models.TransientModel instead of models.Model.
2.Add Fields to the Wizard Model
These fields will store the temporary data entered by the user in each step of the wizard.
3.Create wizard views:
Define the views (forms and buttons) for the wizard in XML files. These views will be displayed to the user during the wizard process.
4.Define wizard Actions
These are methods that get called when the user interacts with the wizard (e.g., clicking a "Next" or "Finish" button).
5.Add wizard to menus or buttons
You can link the wizard to menu items or buttons within forms, allowing users to launch the wizard.
Define Wizard model:
# my_wizard.py
from odoo import models
class MyWizard(models.TransientModel):
_name = 'my.wizard'
_description = 'My Wizard'
name = fields.Char('Name')
age = fields.Integer('Age')
def action_confirm(self):
# create a partner with the entered name and age
partner_vals = {
'name': self.name,
'age': self.age,
}
self.env['res.partner'].create(partner_vals)
return {'type': 'ir.actions.act_window_close'}
Define wizard views:
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<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="My Wizard">
<group>
<field name="name"/>
<field name="age"/>
</group>
<footer>
<button string="Confirm" type="object" name="action_confirm" class="oe_highlight"/>
<button string="Cancel" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_my_wizard" model="ir.actions.act_window">
<field name="name">My Wizard</field>
<field name="res_model">my.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem id="menu_my_wizard" name="My Wizard" parent="base.menu_custom"
action="action_my_wizard"/>
</odoo>
if you want to open wizard while clicking on button then,
basically , you donot need to define action for opening the wizard on clicking on button
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="action_my_wizard" model="ir.actions.act_window">
<field name="name">My Wizard</field>
<field name="res_model">my.wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_my_wizard_form"/>
<field name="target">new</field>
</record>
</odoo>
define button according to requirement
<button name="action_open_wizard" type="object" string="Open Wizard" class="oe_highlight"/>
define wizard model as
from odoo import models, fields, api
class ResPartner(models.Model):
_inherit = 'res.partner'
def action_open_wizard(self):
return {
'type': 'ir.actions.act_window',
'name': 'My Wizard',
'res_model': 'my.wizard',
'view_mode': 'form',
'view_id': self.env.ref('your_module_name.view_my_wizard_form').id,
'target': 'new',
}
How to get default value on wizard from current record
@api.model
def default_get(self, fields):
res = super(StudentExtraInfo, self).default_get(fields)
active_id=self.context.get('active_id') # current record id
brw_id=self.env['school.student.extra'].browse(int(active_id))
if active_id:
res['full_name']=brw_id.full_name
res['department_id']=brw_id.department_id.id # for many2one
'''
country_ids=[]
for rec in brw_id.countries:
country_ids.append(rec.id)
res['countries']=[(6,0,country_ids)] # for many2many
# for one2many
lst2=[]
for rec in brw_id.staff_line_ids:
lst2.append(
0,0,{'name':rec.name,'product_id':rec.product_id.id})
)
res['staff_line_ids']=lst2
'''
return res
How to create anew record from wizard
def create_record(self):
countrys=[]
for rec in self.country_ids:
countrys.append(rec.id)
staff_ids=[]
for rec in self.staff_line_ids:
staff_ids.append(0,0,{
'name':rec.name,
'product_id':rec.product_id.id
})
student=self.env['school.student'].create(
{
'full_name':self.full_name,
'department_id':self.department_id.id, # for many2one
'country_ids':[6,0,countrys], # for many2many
'staff_line_ids':staff_ids, # for one2many
}
)
'''
for creating one2many record you can also do like below: first create student record and link
it with the staff_line_ids like
for rec in self.staff_line_ids
self.env['res.staff.line'].create(
'connecting_field':student.id,
'name':rec.name,
'product_id':rec,product_id.id
)
'''
# create a record an oprn it on edit mode
context=dict(self.env.context)
context['form_view_initial_mode']='edit'
return {
"name":_("create Student"),
"context":context,
"view_mode":"form",
"res_model":"school.student",
"res_id":student.id,
"view_type":"form",
"type":"ir.actions.act_window"
}
Amanda Martines 5 days ago
Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.
ReplyBaltej Singh 5 days ago
Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
ReplyMarie Johnson 5 days ago
Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
Reply