Help

3

How to compare fields in a filter ?

Avatar
Mohamed Hagag

Dears,

Plz let me know how to make this filter work,  adding filter on partners search form:

                    <field name="credit_limit"/>
                    <field name="credit"/>

                   <filter name="credit limit" domain="[('credit','>',credit_limit)]"/>

Applying this filter gives below error:

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\":[]}"}}

I googled many times to find a solution without finding anyone .

the simple form [('credit_limit','<',credit)] always returns the error "can not convert string to float" where string is credit and float is credit_limit.

is there any way to say [('credit_limit','<',valueof(credit))] or [('field1','=',valueof(field2))] ??

Regards,

1 Comment
Avatar
Discard
Avatar
Pascal Tremblay
-

I have the error today!

2 Answers
1
Avatar
Cyril Gaspard (GEM)
Best Answer

Hi,

simply create a stored  boolean function field, which depends of the 2 fields you want to compare, test the operation you need in function, you can now filter by the boolean value.

using last api

in your python file

from odoo import api, fields, models

class ResPartner(models.Model):
    _inherit = 'res.partner'

    credit = fields.Float(string="Credit")
    credit_limit = fields.Float(string="Credit limit")
    is_credit_limit_exceeded = fields.Boolean(compute='_get_is_credit_limit_exceeded',
string="Credit limit exceeded",
compute_sudo=True, store=True)

   @api.multi
    @api.depends('credit', 'credit_limit')
    def _get_is_credit_limit_exceeded(self):
        self.is_credit_limit_exceeded = self.credit > self.credit_limit

in your xml file, in search view

<filter name="Credit limit" domain="[('is_credit_limit_exceeded', '=', True)]"/>
2 Comments
Avatar
Discard
Avatar
Ibrahim Boudmir
-

Without forgetting to add the boolean field with invisible="1"

Avatar
Cyril Gaspard (GEM)
-

Hi,

this is a stored field function with dependecies, no need to add the field in view.

Bye