odooodoo-10

How can I stop Odoo custom button from auto save?


I have created a button inside my view which triggers a method inside the module. However on clicking the button the temporary edited fields are saved and not reverted when clicking the 'Discard' button.

Here is the code for my view:

<form>
  <sheet>
    <group>
    <field name="name" />
    </group>
    <button name="my_button" string="My Button" type="object" class="oe_edit_only" />
  </sheet>
</form>

Once I click my_button the field name is saved in the database and the button Discard has no effect any more.

enter image description here

How can I prevent Odoo from saving the temporary data when clicking my custom button?

(I'm working with Odoo10 but I guess it's the same for older versions of Odoo)


Solution

  • You may be able to change your button into a Boolean field and make your my_button method an onchange.

    Python

    my_button = fields.Boolean('Label')
    
    @api.multi
    @api.onchange('my_button')
    def onchange_my_button(self):
        for record in self:
            # whatever my_button does
    

    If you want it to still display as a button, you can show the label styled as a button and hide the actual checkbox.

    XML

    <label for="my_button" class="btn btn-sm btn-primary"/>
    <field name="my_button" invisible="1"/>