1. 程式人生 > >odoo中獲取_constraint_methods和_onchange_methods

odoo中獲取_constraint_methods和_onchange_methods

@property container implement java 查看 class eth containe script

遍歷所有方法,查看是否有屬性_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


來自為知筆記(Wiz)

odoo中獲取_constraint_methods和_onchange_methods