在Odoo/OpenERP中,我想制作一个过滤器来比较同一对象的字段1和字段2,如下所示。
请让我知道如何使此过滤器工作,在合作伙伴搜索表单上添加过滤器:
<field name="credit_limit"/>
<field name="credit"/>
<filter name="credit limit" domain="[('credit','>',credit_limit)]"/>
应用此过滤器会给出以下错误:
Uncaught Error: Failed to evaluate search criterions:
{"code":400,"message":"Evaluation Error","data":{"type":"local_exception","debug":"Local evaluation failurenNameError: name 'credit_limit' is not definednn{"domains":[[],"[('customer','=',1)]","[('credit','=',credit_limit)]"],"contexts":[{"lang":"en_US","tz":"Africa/Cairo","uid":1,"search_default_customer":1}],"group_by_seq":[]}"}}
我用谷歌搜索了很多次以找到解决方案,但没有找到任何人.
简单形式 [('credit_limit','<',credit)] 总是返回错误"无法将字符串转换为浮点数",其中字符串是 credit,浮点数是credit_limit。
有没有办法说[('credit_limit',<',valueof(credit))]或[('field1','=',valueof(field2))]??
问候
您需要创建一个带有搜索函数的函数字段来执行此操作。
下面是一个使用"旧 api"语法的示例:
class SomeModel(orm.Model):
_name = 'some.model'
def _func_credit_limit_exceeded(self, cr, uid, ids,
field_name, arg, context):
"""compute the value of the function field"""
res = {}
for record in self.browse(cr, uid, ids, context=context):
res[record.id] = record.credit > record.credit_limit
return res
def _func_search_credit_limit_exceeded(self, cr, uid, obj,
name, criterion, context):
"""we only support a search on the form
('column_name', '=', boolean) or ('column_name', '!=', boolean)
"""
match_ids = []
field, op, value = criterion
# argument processing
if op == '!=':
value = not value
elif op != '=':
raise ValueError('Unsupported operator')
# build the search query
if value = True:
comparison = '>'
else:
comparison = '<='
query = 'SELECT id FROM some_model '
'WHERE credit %s credit_limit' % comparison
# get the ids matching the search
# and return the corresponding domain
cr.execute(query)
for row in cr.fetchall():
match_ids.append(row[0])
if match_ids:
# return domain matching the selected ids
return [('id', 'in', match_ids)]
else:
# return a domain which will never yield a result
return [('id', '=', 0)]
_columns = {'credit': fields.float('Credit'),
'credit_limit': fields.float('Credit Limit'),
'credit_limit_exceeded':
fields.function(_func_credit_limit_exceeded,
fnct_search=_func_search_credit_limit_exceeded,
string='Credit Limit Exceeded',
type='boolean'),
}
对于数值字段,您可以创建一个计算字段,用于计算两个字段的差值。 如果结果为 0,则它们相等,如果为负,则第二个更大,如果为正,则第一个更大。