dnl.re dnl.re - 4 months ago 87
Python Question

How to redirect to another form view in python code - Odoo 8

Through clicking a button the user should create a new order from given values and be directly redirected to the form view of the newly created order. Following method is called through the button:

def method_name(self):
vals = [...]
new_order = self.env['sale.order'].create(vals)
self.write({ 'state': 'review', })
return {
'type': 'ir.actions.act_window',
'name': 'sale.view_order_form',
'res_model': 'sale.order',
'res_id': new_order.id,
'view_type': 'form',
'view_mode': 'form',
'target' : 'self',

Sadly nothing happens and I have no clue, what to try next. I tried to change the target to
or the name but nothing changes. Both without success.

Edit: see my comment on Carlos' answer.


To execute a method of your model from a button, you must define the button as a object type like this in your xml view:

<button name="method_name" string="My Button" type="object"/>

Then in your model, if you want to redirect to another view after doing something, you must return the new action:

def method_name(self):
    return {
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'my.model',
        'target': 'current',
        'res_id': the_resource_id,
        'type': 'ir.actions.act_window'