This question has been flagged
2 Replies
8915 Views

I know I can set create=False to hide it from everyone but I would like to be able to see it when I'm in certain group. How can I do it?

Avatar
Discard
Best Answer

Hello dohn_joe,

You can do by overriding fields_view_get method.

here I give you the example of fields_view_get:

For E.g:

    from lxml import etree

    def fields_view_get(self, cr, uid, view_id=None, view_type='tree',

        context=None, toolbar=False, submenu=False):

        if context is None:context = {}

        res = super(class_name, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type,

                    context=context, toolbar=toolbar, submenu=False)

        group_id = self.pool.get('res.users').has_group(cr, uid, 'modulename.group_xml_id')

        doc = etree.XML(res['arch'])

        if group_id:

            if view_type == 'tree':

                nodes = doc.xpath("//tree[@string='Test Tree']")

                for node in nodes:

                    node.set('create', '0')

                res['arch'] = etree.tostring(doc)

        return res

I hope this will helpful for you.

thanks

Avatar
Discard
Author

Thanks a lot! It worked exactly as I wanted.

Always welcome john_joe :) 👍

Best Answer

this has change a bit in Odoo 16 and looks more like this:

def_get_view(self, view_id=None, view_type='form', **options):
arch, view = super()._get_view(view_id, view_type, **options)
if view_type in ('tree'):
nodes = arch.xpath("//tree")
for node in nodes:
# logic needed to switch create on or off               
if True:
node.set('create', '1')
return arch, view



Avatar
Discard