Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

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

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

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

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

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

    Odoo 新手求助
    2
    2
    478
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dennis888 last edited by dennis888

      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里都没找到哦,哪位大牛能帮解答一下?谢谢
      1 Reply Last reply Reply Quote 0
      • 静静
        静静 last edited by

        在js 里面找吧。。。

        1 Reply Last reply Reply Quote 0
        • First post
          Last post