3
votes

I have a small question, just to research. I can hide a field based on attrs in odoo 8, but is there a way to do the same in python code. below is the code:

<field name="test" attrs="{'invisible':[('role', '=', 'testrole')]}" />

so this does the work ( means hides the field if field name role has value 'test role' ) Then i tried to achieve same functionality using python with method onchange on role field as below:

<field name="role" on_change="hide(role)"/>

in my model :

def hide(self,cr,uid,ids,role) :
    res = {'value':{}}
    if role == 'testrole':
       res['value']['test']['attrs']['invisible']=True
    return res

But this does not work, Any suggestions ?

Thanks,

3

3 Answers

3
votes

I prefer the way with a second field, too, but i would choose a computed field instead, like:

role = # your role field definition
hide = field.Boolean(string='Hide', compute="_compute_hide")

@api.depends('role')
def _compute_hide(self):
    # simple logic, but you can do much more here
    if self.role == 'testrole':
         self.hide = True
    else:
         self.hide = False

Now you can use attrs as mentioned by yourself on every other field in that view:

<field name="fieldToHide" attrs="{'invisible':[('hide', '=', True)]}" />
1
votes

In this case you can create a new Boolean field and this field default set False and in your "rol" field invisible={'boolean_filed','=', True} and onchange method also apply and you can apply onchange function the "boolean_field" value set True.

bool = field.boolean('Boolean')

_default { 'bool': False }


def hide(self,cr,uid,ids,role) :
    res = {'value':{}}
    if role == 'testrole':
         res['bool']=True
    return res
0
votes

the answer to your question is in this link. But it's better to use invisible only in the XML code, otherwise it's not going to work properly.

http://stackoverflow.com/questions/31532390/invisible-true-false-parameter-exist-or-not-in-odoo-8