odoo中获取_constraint_methods和_onchange_methods
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了odoo中获取_constraint_methods和_onchange_methods相关的知识,希望对你有一定的参考价值。
遍历所有方法,查看是否有属性_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的主要内容,如果未能解决你的问题,请参考以下文章