odoo中的domain的所有操作符

源码js中有一个映射关系:

C:\Users\dev01\gitproject\odoo12\addons\web\static\src\js\widgets\domain_selector.js

 

var operator_mapping = {
    "=": "=",
    "!=": _lt("is not ="),
    ">": ">",
    "<": "<",
    ">=": ">=",
    "<=": "<=",
    "ilike": _lt("contains"),
    "not ilike": _lt("does not contain"),
    "in": _lt("in"),
    "not in": _lt("not in"),

    "child_of": _lt("child of"),
    "parent_of": _lt("parent of"),
    "like": "like",
    "not like": "not like",
    "=like": "=like",
    "=ilike": "=ilike",

    // custom
    "set": _lt("is set"),
    "not set": _lt("is not set"),
};



但是好多操作符还是不能用在xml视图文件中,网上是这么解释的
We suggest you to use '=' operator instead of ilike. '=' operator works as a =ilike operator.
because Some operators like "ilike, =ilike, like, =like, not ilike,not like" are not supported in "attrs" parameter.


The point is domain calculations (attrs is also using domain) are done in JS not the database, so you can not use SQL commands such as 'ilike'. You can see this discussion in my \blog. 

But there is a way to add this function to Odoo rather easily with some coding. Edit `view_form.js` ( in openerp/addons/web/static/src/js), find this line:

            case 'not in':
                if (!_.isArray(val)) val = [val];
                stack.push(!_(val).contains(field_value));
                break;

and add these lines after it:

            case 'like':
                stack.push(field_value.indexOf(val)>=0);
                break;
            case 'not like':
                stack.push(field_value.indexOf(val)<0);
                break;
            case 'ilike':
                stack.push(field_value.toUpperCase().indexOf(val.toUpperCase())>=0);
                break;
            case 'ilike':
                stack.push(field_value.toUpperCase().indexOf(val.toUpperCase())<0);
                break; 

and save it, TADA you can use it now. 

warning: if you use it with a field which is not string, it will cause an error

Cheers, 

Pouya

更正一下:

操作符ilike还是可以用在attrs中,之前不能用是因为字段的值为false,因此调用indexOf方法报错:

以下为调用源代码:

C:\Users\dev01\gitproject\odoo12\addons\web\static\src\js\core\domain.js

switch (this._data[1]) {
case "=":
case "==":
return _.isEqual(fieldValue, this._data[2]);
case "!=":
case "<>":
return !_.isEqual(fieldValue, this._data[2]);
case "<":
return (fieldValue < this._data[2]);
case ">":
return (fieldValue > this._data[2]);
case "<=":
return (fieldValue <= this._data[2]);
case ">=":
return (fieldValue >= this._data[2]);
case "in":
return _.contains(
_.isArray(this._data[2]) ? this._data[2] : [this._data[2]],
fieldValue
);
case "not in":
return !_.contains(
_.isArray(this._data[2]) ? this._data[2] : [this._data[2]],
fieldValue
);
case "like":
return (fieldValue.toLowerCase().indexOf(this._data[2].toLowerCase()) >= 0);
case "ilike":
return (fieldValue.indexOf(this._data[2]) >= 0);
default:
throw new Error(_.str.sprintf(
"Domain %s uses an unsupported operator",
this._data
));
}
posted @ 2020-01-02 15:07  那时一个人  阅读(760)  评论(0编辑  收藏  举报