Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide field odoo in python onchange method

Tags:

python

odoo

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,

like image 614
user280960 Avatar asked Mar 05 '26 20:03

user280960


2 Answers

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)]}" />
like image 185
CZoellner Avatar answered Mar 07 '26 08:03

CZoellner


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
like image 28
Jainik Patel Avatar answered Mar 07 '26 08:03

Jainik Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!