Odoo - prevent button from closing wizard

Aron Lorincz picture Aron Lorincz · Aug 12, 2015 · Viewed 11.3k times · Source

I have a transient model that serves as a dialog. In my form view I have a button like this:

<footer states="partnerId">
   <button name="check_tax_id" string="Tovább" type="object"/>
</footer>

The button invokes this function (I can confirm it actually invokes):

@api.one
    def check_tax_id(self, context=None):
        self.state = "partnerDetails"

        return None;

My problem is that the dialog window is closed immediately once I click this button! What am I doing wrong?

Answer

ChesuCR picture ChesuCR · Aug 12, 2015

Solution 0

@api.multi
def check_tax_id(self):
    self.ensure_one()
    self.name = "New name"
    return {
        "type": "ir.actions.do_nothing",
    }

This solution was provided here by Tadeusz Karpinski.

Solution 1

You can return a new form with the same record id.

@api.multi
def check_tax_id(self):
    self.ensure_one()
    self.name = "New name"
    return {
        'context': self.env.context,
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'model_name',
        'res_id': self.id,
        'view_id': False,
        'type': 'ir.actions.act_window',
        'target': 'new',
    }

Solution 2

You can create a widget in jQuery. This will open the wizard and you can assign the behaviour you want to the buttons manually. You can use the call function to call python functions as well:

[...]

new instance.web.Dialog(this, { 
    title: _t("Title"), 
    width: '95%', 
    buttons: [
          { text: _t("First button"), click: function() { self.first_button(); }}, 
          { text: _t("Second button"), click: function() { self.second_button(); }},
          { text: _t("Close"), click: function() {  dialog.close(); }},                       
      ],
});

[...]

Solution 3

Of course you can override the create method as well to avoid the creation of the record in some cases

Solution 4

One last option. Create a workflow with a state field. Create workflow buttons in order to send signals to change the state. You can show or hide the rest of the fields using the attrs attribute and the state field. But I do not know if that would adapt to your needs.