0
votes

I need to hide or show a button depending on whether or not a Char field has a specific string in it. It seems that the 'like' operator would be perfect. In my xml, I have:

 <record model="ir.ui.view" id="my_view">
     <field name="name">my.form</field>
     <field name="model">mymodule.my</field>
     <field name="arch" type="xml">
         <form string="My Form">
             <header>
                 <button name="test_like_1" type="object"
                         string="Should not appear"
                         attrs="{'invisible':[('state2','like','measure')]}"
                 />
                 <button name="test_like_2" type="object"
                         string="Should appear"
                         attrs="{'invisible':[('state2','not like','measure')]}"
                 />
...

State2 contains 'measure,prelien', so I expect that the first button will be invisible and the second visible. However, both are invisible.

What am I missing?

Edit

I ran the query that I think Odoo would create from this domain -

select id, description, state2 from mymodule_my where state2 like '%measure%';

It runs as expected, returning the records that have "measure" as a substring. Somehow, this SQL isn't being generated/used. My next step is to dig through the Odoo code & see what's happening.

Can anyone provide insight to what's going on?

3
@Kenly - I've read through that several times. From what I can understand in that answer, what I have should work, but it doesn't. Feels like I'm missing something obvious, but I can't find it.Guy

3 Answers

1
votes

I found the problem - available operators for attrs in a view describes it best and outlines one possible solution. The gist is that the domains specified in attrs are evaluated in javascript on the client. The 'like' & 'ilike' operators aren't implemented.

You can verify this by viewing the console. In my case, I got a ton of these warnings -

Unsupported operator ilike in domain [["state2","ilike","measure"]]

I'm looking into either extending the compute_domain function in odoo/addons/web/static/src/js/framework/data.js as suggested1 or simply working-around the limitation.

0
votes

You could try

attrs="{'invisible':[('state2','in',['Measure','MEASURE','measure'])]}"

And

attrs="{'invisible':[('state2','not in',['Measure','MEASURE','measure'])]}"

You may have to add more items to your lists. I am not sure if like and not like are supported but this is a method I see used in other addons.

0
votes

You're comparing the string 'state2' instead of the value of the field state2 also you should be comparing the other way around, this isn't exactly the best example but you should get what i mean.

>>> 'measure,prelien' in 'measure'
False
>>> 'measure' in 'measure,prelien'
True
>>> 

The first condition will never evaluate to true. you should do this instead

define a char field named default, and set it's default value to 'measure' and make it hidden on the view

measure = fields.Char('Measure', default='measure', store=False)

Then your view should look like this

<field name="measure" invisible="1" />

<button name="test_like_1" type="object"
                         string="Should not appear"
                         attrs="{'invisible':[('measure', 'like', state2)]}"
                 />
<button name="test_like_2" type="object"
                         string="Should appear"
                         attrs="{'invisible':[('measure', 'not like', state2)]}"
                 />