简体   繁体   中英

odoo10 how to build 2 onchange function in 1 field

I have problem with an onchange function in Odoo 10.

This is some example code:

class test_1(models.Model)
    Input = fields.Integer()

From another class, I have an onchange function. This class is a default system, which can't change or add anything in this code.

class test_onchange(models.Model)
    @api.onchange('Input')
    def _onchange_test_1(self):
        ## some process ##

Here is the function I am writing. It is an onchange with Input too.

class test_onchanger(models.Model)
    @api.onchange('Input')
    def _onchange_test_addon(self):
        ## some process. and different  _onchange_test_1

So the question is:

How can I build an onchange function on the Input field from another class/module if the Input field already has an onchange function from the default system/code.

Anyone have an idea? Can I inherit on _onchange_test_1 or maybe something else?

To confirm, you want to extend (add customizations onto) an existing onchange method?

As mentioned in the other answer, the simplest way is to just inherit the existing onchange method.


Let's assume this is the core code, which you can't modify:

class CoreClass(models.Model):
    _name = 'core.class'

    Input = fields.Integer()

    @api.onchange('Input')
    def _onchange_input(self):
        ## some process ##

In your module, you can extend the class and make your additional changes.

Note: The core class method still runs when you call it with super .

class CoreClassCustom(models.Model):
    _inherit = 'core.class'

    @api.onchange('Input')
    def _onchange_input(self):
        res = super(CoreClassCustom, self)._onchange_input()
        ## your custom process ##
        return res

Here is the Odoo 10 ORM Documentation , which briefly goes over onchange methods.

您可以继承现有的onchange方法或为同一字段编写另一个onchange方法,两者都可以工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM