Hello , In this post were going to talk about wizards how to create and use them.
A wizard is a model that display a form view to the user usually in a dialog window, with some fields to be filled in, unlike other models wizards are declared using models.TransietModel and data used in wizard it’s expected to be useful in only until wizard is completed or canceled.
The class TransientModel extends Model and reuse all its existing mechanisms, with the following particularities (Odoo Wizard Documentation):
- Wizard records are not meant to be persistent; they are automatically deleted from the database after a certain time. This is why they are called transient.
- Wizard models do not require explicit access rights: users have all permissions on wizard records.
- Wizard records may refer to regular records or wizard records through many2one fields, but regular records cannot refer to wizard records through a many2one field.
Declaring wizard is easy as declaring normal module the only difference is the class definition ,unlike other model we use models.TransietModel to declare wizard ,wizards are stored in memory instead of memory.
Here in our example we are going to assign the level to a number of students at once instead one each one of them ,in this cases the wizard is useful to reduce the time an make it easy to change properties and information for mass record.
in the wizard we defined 2 fields and 2 functions:
student_ids : Many2many field which will contain a list of selected students.
level : Char field to contain the level to assign to the list of students
the _get_default_student function will browse the existing record in students model that correspond to the selected student. the active_ids key is the list of IDs selected in the tree/list view.
we defined set_student_level which we are going to use in the button wizard (next paragraph). this function will loop the field student_ids and then assigned the level to each one of them.
defining the view in wizards is like the same as other models ,here is the form view of our wizard:
in our wizard were going to use 2 buttons:
Update Level button which will call the set_student_level defined previously and a button to cancel or close the wizard.
here is the result :
to launch a wizard we should create an action like the following:
id : is the unique id defining the action
name : the name of the wizard
res_model : is the name of the wizard model
src_model : is the target module where the wizard will appear when we click the action button.
view_mode : the default mode view in this case form view.
target : there is 2 option for target attribute “new” or “current” ,we use “new” here to open the form in a new window instead of the current one.
after defining the action we can see under the action button the wizard “Student Level”.
we’re going to select 3 student and assigned them a level here is the records before operation:
first we should select a least one line in order to open the wizard otherwise the system will throw an error , the we enter the level and click on update level button :
finally you can see that are the line are updated with the same level :).
this is it for wizard if you didn’t find something clear and you want more explanation you can leave a comment here or contact us.
you can access the code from our github : https://github.com/odooforbeginners/blogtuto