0

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?

Guy
  • 308
  • 2
  • 11
  • Look at http://stackoverflow.com/questions/29442993/available-domain-operator-in-openerp-odoo – Kenly Sep 06 '16 at 21:12
  • @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 Sep 07 '16 at 00:26

3 Answers3

1

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.

Guy
  • 308
  • 2
  • 11
0

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.

Phillip Stack
  • 3,308
  • 1
  • 15
  • 24
  • That doesn't work - I need to know if state2 has the string 'measure' in it, not whether it is a variant of 'measure'. – Guy Sep 06 '16 at 23:27
  • You could hack your way around the issue. Dont get me wrong, I think like should work. I couldnt find an example of it in the source code with the exception of domains and not attrs. To work around this issue you could just make a generic button which handles both functions. So the button executes a function which evaluates for the presence of 'measure' and appropriately redirects to the actual function you want to run or you combine both functions into one and have an if statement which checks for the string and simply runs the appropriate code. – Phillip Stack Sep 07 '16 at 01:48
0

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)]}"
                 />
danidee
  • 9,298
  • 2
  • 35
  • 55
  • I tried that before. It gives this error - Uncaught Error: Unknown field measure in domain [["measure","ilike","state2"]] – Guy Sep 06 '16 at 23:13