4
votes

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)

2
finding the same...Alpesh Valaki
What does your my_button method do? You may be able to change your button into a Boolean field and make your my_button method an onchange.travisw
I think the idea with a boolean field sounds interresting, but how do I make my boolean field look like a button? I know there is the widget="boolean_button" but it is again surrounded by a button - and hence triggers the same behaviour (it saves my view). But maybe I can use this idea - showing a checkbox...IstaLibera
@travisw thank you for your help. Using a boolean field instead of a button was a good solution for my purpose.IstaLibera
@travisw: I suggest you to post that comment as an answer, it might helps to others.Emipro Technologies Pvt. Ltd.

2 Answers

5
votes

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"/>
1
votes

By default in odoo while any of the server side code will render once you trigger any event (Like button click) then record will be saved first anyhow and you will get the record in Self (Invoking object).

So once you click on that button it means that record has been saved in database and there will no effect of Discard after that.

The best example you can see in the Sales Quotation / Order there is one link "Update" which will just return True from the method it performs nothing but once that method will be called then the entire record will be saved and totals (all functional fields will be calculated) and you feel that update link performs calculation (that link is visible in edit mode only).

Generally in new api methods which are called from the button click should be having decorators @api.one or @api.multi.

##Single record will be there in self.
@api.one
def button_click(self):
    return False

##list of records (recordset) will be there in self.
@api.multi
def button_click(self):
    return False

So when you click that button that record save first and then method will be called.