简体   繁体   中英

Odoo 8 onchange on inherited models

I have a module that implements an onchange method on res.partner. If I create a new model that inherits res.partner, the onchange is not called. Is there a way to make the onchange general, so it's also called on inherited models?

Example:

class ResPartner(models.Model):
    _inherit = 'res.partner'

    @api.onchange('zip')
    def _valid_zip(self):
        print 'Validating zip...'

class ExtendedPartner(models.Model):
    _name = 'extendedpartner'
    _inherits = {'res.partner': 'partner_id'}

If I change the zip code on an extendedpartner, the onchange is not called.

You use delegation inheritance in the code above. Delegation inheritance does not work on model methods. It just simply delegates lookup of fields not found in current model to the "parent" model.

I think what you want is prototype inheritence:

class ExtendedPartner(models.Model):
    _name = 'extendedpartner'
    _inherit = 'res.partner'

The graphic below shows three types of inheritance available in Odoo:

Odoo中提供三种继承

You currently use the first one ("classic inheritance") in ResPartner (which inherits from res.partner ) and the last one (delegation inheritance) in ExtendedPartner . I think the middle one (Prototype inheritance) would be more appropriate for ExtendedPartner . It basically works in a manner very similar to standard Python inheritance.

You can read more about different types of inheritance in the documentation (which is also the source of the image above).

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