How can I stop Odoo custom button from auto save? - odoo

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.
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)

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

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.

Related

PayPal Variables Add ref number

I'm struggling to add a variable to my PayPal payment form I need users to add a ref number so that I can identify the order so for example,
I normally use the email link option rather than the form option
Add Ref No :
Add Personal Message :
any help would so much appreciated
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="HUMXQRA7T9Z3L">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
You're using a hosted button, so edit the button at www.paypal.com/buttons , view your saved buttons , find your button and edit it using the action menu.
In the Customize Button box, check the option to add a text field, and save your changes. This will generate new code to copy.
The hosted editor will only generate one text field, so if you need a button with more you'll need to create a new button with the first text field, and in step 2 uncheck the option to save the button at PayPal. When the code is created, click to remove the code protection. This will result in an unhosted button. Copy it to a text editor, and you can then extend the single text field to be two text fields, incrementing the ID number from e.g. 0 to 1 as is necessary for the second field.
You can read about HTML variables for PayPal buttons.

trigger (ngOnChange) by code to bind the changed model value

I have an input field that takes a date and use pipe to change the output accordingly and with that I am using
(ngOnChange)="myvalye=myFunction($event)"
to update the value. now I have a button whose onclick i want to reset all the data. Below is the code for refrence.
<mydatepicker [ngModel]="mydate | datepipe" (ngModelChange)="mydate=myFun()"/></mydatepicker>
<button class="btn btn-default" (click)="reset()">Reset</button>
so on click of reset button myValue is not getting changed in input box.
datepicker library can be found here

Create Radio Button condition in Odoo 9

I'm new learning about odoo.
I have a module with two class, and in my XML, i just wanna make a radio button for part of the class, for example on individual and company radio button, I want to make radio button with a different button which shows different fields from two class.
Thank You.
To do this, You must create a field Selection. In the XML of this field, you specify the widget radio button.
After for each field that is to be displaying in the view, you must add an attribute on the xml field to hide or display the value.
Example :
Python file
type = field.Selection(string='Type',selection=selection=[('val1', 'Val1'), ('val2', 'Val2')])
field1 = fields.Char(string='Field1')
field2 = fields.Char(string='Field2')
Xml file
<field name="type" widget="radio"/>
<field name="field1" attrs="{'invisible': [('type','=', 'val1'))]}"/>
<field name="field2" attrs="{'invisible': [('type','=', 'val2'))]}"/>
In this example, if the radio button has the value "val1" only the field1 is displaying. And if the radio button has the value 'val2' only the field2 is displaying.

How to use a confirmation dialog box in Odoo?

I'd like to get a value in a confirmation dialog bow in odoo.
I have a form with a select. When I choose a value in the select and click the save button, I have a diablog box with a message. In this message I'd like to get the selected value.
Any idea ?
<button string="save"
icon="gtk-ok"
type="object"
name="update_year"
confirm="The selected year is XXXX. Confirm ?"/>

Hide edit button according to status in openerp

I want to hide the edit button from the form in my module.But it should hide after changing the status to 'done'.I have three states for record (new,draft,done).If it is in 'draft' status I want to show the edit button . But if it is on 'done' state need to hide the edit button. I tried this
<form string="Consumption Result" edit="false" version="7.0">
But this cause always hide the edit button . How can I give condition here.
You can provide states="draft" for the button "edit" like below:
<button name="case_edit" string="Edit" type="object" states="draft"/>
with this definition the button is available when the states="draft" and not available at "new" and "done".