码迷,mamicode.com
首页 > 其他好文 > 详细

odoo中获取_constraint_methods和_onchange_methods

时间:2017-09-10 10:14:49      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:inverse   int   方法   map   res   methods   ppi   code   mapping   

遍历所有方法,查看是否有属性_constrains或是_onchange,如果有的话则说明是_constraint_methods或_onchange_methods
    @property
    def _constraint_methods(self):
        """ Return a list of methods implementing Python constraints. """
        def is_constraint(func):
            return callable(func) and hasattr(func, ‘_constrains‘)

        cls = type(self)
        methods = []
        for attr, func in getmembers(cls, is_constraint):
            for name in func._constrains:
                field = cls._fields.get(name)
                if not field:
                    _logger.warning("method %s.%s: @constrains parameter %r is not a field name", cls._name, attr, name)
                elif not (field.store or field.inverse or field.inherited):
                    _logger.warning("method %s.%s: @constrains parameter %r is not writeable", cls._name, attr, name)
            methods.append(func)

        # optimization: memoize result on cls, it will not be recomputed
        cls._constraint_methods = methods
        return methods

    @property
    def _onchange_methods(self):
        """ Return a dictionary mapping field names to onchange methods. """
        def is_onchange(func):
            return callable(func) and hasattr(func, ‘_onchange‘)

        cls = type(self)
        methods = defaultdict(list)
        for attr, func in getmembers(cls, is_onchange):
            for name in func._onchange:
                if name not in cls._fields:
                    _logger.warning("@onchange%r parameters must be field names", func._onchange)
                methods[name].append(func)

        # optimization: memoize result on cls, it will not be recomputed
        cls._onchange_methods = methods
        return methods




odoo中获取_constraint_methods和_onchange_methods

标签:inverse   int   方法   map   res   methods   ppi   code   mapping   

原文地址:http://www.cnblogs.com/crax/p/7500146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!