Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
6 Replies
22652 Tampilan

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"

Avatar
Buang
Jawaban Terbai

Try this:

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

Regards

Avatar
Buang
Jawaban Terbai

Use

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

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

Avatar
Buang
Penulis

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

Jawaban Terbai

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

Avatar
Buang
Jawaban Terbai

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. 


Avatar
Buang
Penulis Jawaban Terbai

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

Avatar
Buang