Search code examples
pythonodooodoo-8odoo-view

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:

@api.one
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 new or current or the name but nothing changes. Both without success.

Edit: see my comment on Carlos' answer.


Solution

  • 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:

    @api.multi
    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'
        }