openerp域过滤器如何比较对象中的2个字段?

时间:2014-12-07 19:17:30

标签: filter openerp odoo

在Odoo / OpenERP中我想制作一个过滤器来比较同一个对象的field1和field2,如下所示。

Plz让我知道如何使这个过滤器工作,在合作伙伴搜索表单上添加过滤器:

                <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 failure\nNameError: name 'credit_limit' is not defined\n\n{\"domains\":[[],\"[('customer','=',1)]\",\"[('credit','=',credit_limit)]\"],\"contexts\":[{\"lang\":\"en_US\",\"tz\":\"Africa/Cairo\",\"uid\":1,\"search_default_customer\":1}],\"group_by_seq\":[]}"}}

我google了很多次,找到了一个没有找到任何人的解决方案。

简单形式[('credit_limit','&lt;',credit)]总是返回错误“无法将字符串转换为浮点数”,其中字符串为credit,float为credit_limit。

有没有办法说[('credit_limit','&lt;',valueof(credit))]或[('field1','=',valueof(field2))] ??

此致

2 个答案:

答案 0 :(得分:0)

您需要使用搜索功能创建一个功能字段来执行此操作。

以下是使用“旧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'),
                 }

答案 1 :(得分:0)

对于数字字段,可以创建一个计算字段,该字段计算两个字段的差。如果结果为0,则它​​们相等;如果为负,则2nd较大;如果为正,则2nd较大。