How To create wizards in odoo ?

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.

Declaration:

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.

selection_724

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.

Wizard View:

defining the view in wizards is like the same as other models ,here is the form view of our wizard:

Selection_725.png

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 :

Selection_726.png

Launch wizard:

to launch a wizard we should create an action like the following:

Selection_729.png

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”.

Selection_730.png

Demo :

we’re going to select 3 student and assigned them a level here is the records before operation:

selection_730

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 :

selection_733

finally you can see that are the line are updated with the same level :).

selection_734

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s