Skip to Content
Menu
This question has been flagged
1 Reply
4462 Views

i have a form view which contains 6 tabs of personal as well as other informations provided by the manager.

i want the informations provided by the managers i.e contained in a tab should appear as readonly but the personal informations will be read and write access.How can i achieve so..

Avatar
Discard
Best Answer

You can use fields_view_get method for that. In this method you need to check the current user's group and based on that you can make the field readonly / editable.

1st Option:

Try following code:

from lxml import etree
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
result = super(AccountInvoice, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=submenu)
doc = etree.XML(result['arch'])
if self._uid != SUPERUSER_ID and (not user.has_group('module_name.your_group_id'):
if doc.xpath("//field[@name='field_name']"):
node = doc.xpath("//field[@name='field_name']")[0]
modifiers = json.loads(node.get("modifiers", '{}'))
modifiers.update({'readonly': True})
node.set("modifiers", json.dumps(modifiers))
result['arch'] = etree.tostring(doc)
return result

2nd Option:

Yes, you can do it by compute method as well. Create a boolean field with compute and make it True or False based on the user' group (check by user.has_group method).
Now, use boolean field in attrs to make the field readonly.

Your compute field must not be store=True and your compute method must use api.multi only and not api.depends. Because we want to check the group of the logged in user each time the record is opened.

Avatar
Discard

Hello sudhir,

I have little bit query like can we apply above conditions in compute method that's is possible. ?

Yes, you can. Please check my updated answer option2.