Skip to Content
Menu
This question has been flagged
5 Replies
8788 Views

HI, 

I know that is not possible hide a field in tree view with attrs={"invisible":[('field_name','=',True)]}, and it is possible using: invisible attribute like this: invisible="context.get('field_name',False)", but I want to know how I can pass a field value through context from model to view, Ex:

model.py

admin_user = fields.Boolean(string="Name", default=True)


view.xml

<tree>

    < field name="field_name" invisible="context.get('admin_user',False)"/>

</tree>


Any example for this question?,

Thanks

Avatar
Discard
Best Answer

You can hide your field according to your conditions via following steps.
Note : You want to import

from lxml import etree

 In your Python code:


in your xml add invisible=0 with your field in xml:


 <tree>      

    < field name="field_name" invisible="0"/>   

</tree>


I think it may hep you

Avatar
Discard

why if view_type == 'form ??

does it work in odoo11

Best Answer
    def fields_view_get(self, view_id=None, view_type='tree', toolbar=False, submenu=False):
result = super(ThisModel, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
if not self.env.context.get('show_the_column', False) and view_type == 'tree':
doc = etree.fromstring(result['arch'])
for field in doc.xpath('//field[@name="name_of_conditional_column"]'):
field.set('invisible', '1')
modifiers = json.loads(field.get('modifiers', '{}'))
modifiers['tree_invisible'] = True
modifiers['column_invisible'] = True
field.set('modifiers', json.dumps(modifiers))
result['arch'] = etree.tostring(doc)
return result
Avatar
Discard
Best Answer

Hi,

you can update the context by calling super of fields_get function from the model of tree view.Can update context by 

context.update({'admin_user':False}) and pass the updated context in super

Avatar
Discard
Author

Hi sarga,

maybe do you have any example by this way, because I don't know how to use this function (fields_get), Thanks.

Hi,

I just show you how I just used fields_get in sale order model to update context.

def fields_get(self,cr,user,all fields=None,context=None,write_access=True,attributes=None):

context_copy=context.copy()

context_copy.update({'admin_user':False})

return super(so,self).fields_get(cr,user,allfields,context_copy, write_access,attributes)