Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

onchange装饰器具体是如何触发的?



  • 1.示例
    from odoo import models, fields,api
    class AModel(models.Model):
    _name = 'a.model.name'
    field1 = fields.Char()
    field2 = fields.Integer(string="an other field")

    @api.onchange('field1', 'field2') # 当这两个字段值改变时调用该函数
    def check_change(self):
    if self.field1 < self.field2:
    self.field3 = True

    2.api.py
    def onchange(*args):
    """ Return a decorator to decorate an onchange method for given fields.
    Each argument must be a field name """
    return attrsetter('_onchange', args)

    def attrsetter(attr, value):
    """ Return a function that sets attr on its argument and returns it. """
    return lambda method: setattr(method, attr, value) or method

    '''
    The following attributes are used, and reflected on wrapping methods:

    • method._onchange: set by @onchange, specifies onchange fields
    • method.clear_cache: set by @ormcache, used to clear the cache

    On wrapping method only:

    • method._api: decorator function, used for re-applying decorator
    • method._orig: original method
      '''

    WRAPPED_ATTRS = ('module', 'name', 'doc', '_constrains',
    '_depends', '_onchange', '_returns', 'clear_cache')

    INHERITED_ATTRS = ('_returns',)

    1. 问题:
      请问当'field1', 'field2'字段值发生变化时,是如何触发调用装饰器函数的?_onchange 是fields类的一个内部方法吗?可是在api,fields和models里都没找到哦,哪位大牛能帮解答一下?谢谢


  • 在js 里面找吧。。。