装饰器会给函数添加个 _constrains 属性,所以根据这个就可以找到所有的约束函数
xxxxxxxxxx
1
@property
2
def _constraint_methods(self):
3
""" Return a list of methods implementing Python constraints. """
4
def is_constraint(func):
5
return callable(func) and hasattr(func, '_constrains') // 判断条件
6
7
cls = type(self)
8
methods = []
9
for attr, func in getmembers(cls, is_constraint):
10
for name in func._constrains:
11
field = cls._fields.get(name)
12
if not field:
13
_logger.warning("method %s.%s: @constrains parameter %r is not a field name", cls._name, attr, name)
14
elif not (field.store or field.inverse or field.inherited):
15
_logger.warning("method %s.%s: @constrains parameter %r is not writeable", cls._name, attr, name)
16
methods.append(func)
17
18
# optimization: memoize result on cls, it will not be recomputed
19
cls._constraint_methods = methods
20
return methods
已复制