コンテンツへスキップ
メニュー
この質問にフラグが付けられました
6 返信
22714 ビュー

I need something like "not contains" for attrs of a field

I have already tested:

<field name="my_field" attrs="{'invisible': [('other_field', 'not like' , 'blabla')]}"/> <field name="my_field" attrs="{'invisible': [('other_field', 'not ilike' , 'blabla')]}"/> <field name="my_field" attrs="{'invisible': [('other_field', 'not contains' , 'blabla')]}"/>

The field is everytime hidden. Other field has type="char"

アバター
破棄
最善の回答

Try this:

<field name="your_field" 
       attrs="{'readonly': ['!', ('other_field', 'ilike', 'blabla')]}"/>

Regards

アバター
破棄
最善の回答

Use

<field name="my_field" attrs="{'invisible': [('other_field', 'not in' , ['blabla'])]}"/>

Put the values in the list. And use the operator not in

アバター
破棄
著作者

it doesn't work, because the value of the field contains also other characters. i.e. 100245-45-(blabla)

最善の回答

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

アバター
破棄
最善の回答

I'm facing the same problem. Seems 'attrs' works fine just with '=' or '!=' but when you try to filter a field looking for a string, or an array of results, is quite unhelpful. Create a new field using python shows how some functions are still not well implemented/described in Odoo, even if the rest is superb. 


アバター
破棄
著作者 最善の回答

I solved it by using another boolean field and on_change.

python:

_columns = {
 'wf_copy_product':fields.boolean('Copy of Product'),
}

def onchange_default_code(self, cr, uid, ids, default_code, context=None):
            result={}
            if 'blabla' in default_code:
                result['wf_copy_product'] = True
...

xml:

<field name="wf_readonly" invisible="1"/>
<field name="default_code" on_change="onchange_default_code(default_code, context)" required="1" attrs="{'readonly': [('wf_readonly','=', True)]}"/>

hope it help

アバター
破棄