zoukankan      html  css  js  c++  java
  • odoo中的domain的所有操作符

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

    C:Usersdev01gitprojectodoo12addonswebstaticsrcjswidgetsdomain_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 log. 

    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:Usersdev01gitprojectodoo12addonswebstaticsrcjscoredomain.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
    ));
    }
  • 相关阅读:
    set与map
    统计一个字符串中出现了多少个不同的字符
    求序列中所有不同的连续子串的数量
    79、idea IDE Eval Reset
    78、idea控制台报 java: 无效的目标发行版: 14
    16、docker安装minio
    77、idea中添加maven项目右侧无maven
    76、mysql5.7安装教程
    74、js向上递归
    72、解决IntelliJIDEA没有Spring Initializr
  • 原文地址:https://www.cnblogs.com/qianxunman/p/12133069.html
Copyright © 2011-2022 走看看