Odoo Help


why my onchange event is not fired?

Pavel Pavlov
on 11/11/13, 7:57 AM 6,105 views


I've added in the form view for the invoices additional filed "Total In Words".

See picture:

image description

I've also written following piece of code

from openerp import tools
from openerp.osv import fields, osv
from openerp.tools.translate import _
from tools.translate import _
from tools.amount_to_text_en import amount_to_text

#Import the logger for debug code
import logging
_logger = logging.getLogger(__name__)

class account_invoice(osv.osv):
    _inherit = 'account.invoice'
    _columns = {
                'zTotalInWords': fields.char('Total In Words', size=100, readonly=True, states={'draft': [('readonly', False)]}),                

    def onchange_amount(self, cr, uid, ids, amount):
        text_amount = amount_to_text_en.amount_to_text(amount, 'en', 'EURO')
        _logger.debug(self, 'zFS_account_invoice:account_invoice: amount: %s; text_amount: %s', amount, text_amount)
        return {'value': {'zTotalInWords': text_amount}}

and created following xml file

<?xml version="1.0" encoding="utf-8"?> <openerp>
    <data>      <record id="zinvoice_form" model="ir.ui.view">
            <field name="name">account.invoice.form.inherited</field>
            <field name="model">account.invoice</field>
            <field name="inherit_id" ref="account.invoice_form"/>          

            <field name="arch" type="xml">
                <field name="amount_total" position="replace">
                    <field name="amount_total" on_change="onchange_amount(amount_total)" class="oe_subtotal_footer_separator" widget="monetary" options="{'currency_field': 'currency_id'}"/>       
                <field name="amount_tax" position="after">
                    <field name="zTotalInWords"/>
    </data> </openerp>

Still, I don't get my logger.info message in the server log and the field does not get updated automatically and I don't get error... I suspect that the event is not fired at all so my py code is not executed... Same when I hit the "update" button, which updates the total price in numbers...

What did I do wrong and how to fix it?

Regards, Pavel Pavlov

I don't think that total has a onchange method since it's a function field. Maybe you can override the function.

GH Mediacloud, Grover Menacho
on 11/11/13, 5:10 PM

Hi Grover, have you seen some docu or examples on that? I'm not sure I understand your proposal... My understanding is that I can add an OnChange method via mu zinvoice_form in the XML file. Thank you! Regards, Pavel

Pavel Pavlov
on 11/22/13, 3:38 AM
On 11/23/13, 5:53 AM

Hi Pavel,

To convert amount to text, you can use the amount_to_text method defined in openerp/tools/amount_to_text_en.py

Create a field which will store text value of amount. You can either create char or text field.

 'text_amount': fields.char("TextAmount", size=100)


'text_amount': fields.text("Text Amount")

Now write a on_change method which will be called when you change the value of amount field. First add on_change in your xml where your amount field is.

Like this:

<field name="your_amount_field" on_change="onchange_amount(your_amount_field)"/>

Write the following code inside your class in your py:

from openerp.tools import amount_to_text_en

def onchange_amount(self, cr, uid, ids, amount):
    text_amount = amount_to_text_en.amount_to_text(amount, 'en', 'EURO')
    return {'value': {'text_amount': text_amount}}

This solution is suggested by Sudhir Arya

Pavel Pavlov
On 11/22/13, 3:40 AM

Still haven't found any solution, so any help is appreciated. Thank you!

On 11/22/13, 9:06 AM

Hi Pavel, You should not define on_change on function field, it will not be called. For your scenario you should override the function which is called to count 'amount_total'. Want to add one more thing the value fetched from on_change can't be saved b'coz you have field with readonly attributes. Values with readonly attribute didn't get in vals on 'write' method.

Thanks, Ajay Chauhan

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/11/13, 7:57 AM
Seen: 6105 times
Last updated: 9/22/17, 1:46 AM