Thursday, 4 August 2016

Show or Call Wizard from Python Code Odoo


Hello world. You know this is for me to refresh my brain at work. Today i just got task, very simple but sometimes we need to create with similar case that i have. So i just want to show or calling wizard, but its not from xml code but on the python code. On the previous post here, i just show wizard on the xml code, but now i try to show you different way its from python code. Lets do it.

So as you know wizard is model, but they have type TransientModel, it means they just save in temporary memory not to primary memory (database).  Ya i just want you to know it, its first principle that i don't know before my friend told me. Lol.

This is my function to call wizard :

@api.multi
def call_wizard(self):
    print "call wizard fucntion"
    wizard_form = self.env.ref('custom_wizard.cutom_wizard_view', False)
    view_id = self.env['dedy.yuristiawan.wizard']
    vals = {
                'name'   : 'this is for set name',
            }
    new = view_id.create(vals)
    return {
                'name'      : _('Hi I am wizard, I am from python code'),
                'type'      : 'ir.actions.act_window',
                'res_model' : 'dedy.yuristiawan.wizard',
                'res_id'    : new.id,
                'view_id'   : wizard_form.id,
                'view_type' : 'form',
                'view_mode' : 'form',
                'target'    : 'new'
            }

As you can see, on the function i can get conclusion like this :

1. Declaration your wizard, like where is the wizard position. like this one :

wizard_form = self.env.ref('custom_wizard.cutom_wizard_view', False)

I just declaration my module custom_wizard and the template id cutom_wizard_view and then i put to variable wizard_form .   

2. Create record to get id wizard. like this one : 

        view_id = self.env['dedy.yuristiawan.wizard']
    vals = {
                'name'   : 'this is for set name',
            }
    new = view_id.create(vals)

in this code, i just call model and then i do create one record, set name (cause in my wizard model i just have one field is name ). And then i put to variable new . This variable is for get id, i can call with new.id . 

3. Return Your function, to call wizard. Use this code :

return {
                'name'      : _('Hi I am wizard, I am from python code'),
                'type'      : 'ir.actions.act_window',
                'res_model' : 'dedy.yuristiawan.wizard',
                'res_id'    : new.id,
                'view_id'   : wizard_form.id,
                'view_type' : 'form',
                'view_mode' : 'form',
                'target'    : 'new'
            }

name is for title wizard, 
type for call action window, 
res_model is for set our wizard model, 
res_id is id record that we create before. 
view_id is view that we create before, 
view_type and view_mode is to set from for wizard, 
and the last is target new for create new action. 

Just it, you can read first, if you still confuse about my code you can fill comment bellow. I hope this code can help you little bit.


Share:
Lokasi: Semarang, Semarang City, Central Java, Indonesia

13 comments:

  1. It can created successfully. But even return wizard, it show nothing.

    ReplyDelete
  2. it's not working
    It can created successfully. But even return wizard, it show nothing.

    ReplyDelete
  3. This way not working

    ReplyDelete
  4. I do that but it show nothing whats wrong?

    ReplyDelete
  5. I need to call a wizard from the post_init_hook , is there any way to do that?

    ReplyDelete
  6. This will work to open wizard from py.

    def action_open_wizard(self):
    view_id = self.env.ref('module.wizard_template_view_id').id
    return {'type': 'ir.actions.act_window',
    'name': _('View name that you want to display'),
    'res_model': 'wizard.model',
    'target': 'new',
    'view_mode': 'form',
    'views': [[view_id, 'form']],
    }

    Make sure to create wizard template and its action in xml.

    Regards,
    Dhvanil Trivedi

    ReplyDelete
  7. I tried this way but it didn't work how can i achieve this ?


    view_id = self.env.ref('purchase_order_task.view_po2projecttask_wizard').id
    print(view_id)
    return{'type':'ir.actions.act_window',
    'name': _('Convert to Task'),
    'res_model':'po.to.task',
    'target':'new',
    'view_mode':'form',
    'views':[[view_id,'form']],
    }

    ReplyDelete
  8. Great informative article! Thanks for sharing this informative article. Looking for the best Web app development company in Australia? Reach IDYA Technology.

    ReplyDelete


<br>


Advice for contacting me at dedyy54@gmail.com