Skip to Content
Menu
This question has been flagged
2 Replies
11189 Views

I want to set all fields readonly in specific state in form view only. i inherit the model and try to make fields readonly using fields_view_get() method. i don't want to use attributes in xml view. in Odoo v12 it's working with use of setup_modifiers().

Avatar
Discard

Have a look into customization tips: https://goo.gl/8HgnCF

Best Answer

Hi,

You can try the following code

import json
from lxml import etree

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(YourClass, self).fields_view_get(view_id=view_id,
view_type=view_type,
toolbar=toolbar,
submenu=submenu)

if (your_condition):
doc = etree.XML(res['arch'])
    for field in res['fields']:
    for node in doc.xpath("//field[@name='%s']" % field):
    node.set("readonly", "1")
            modifiers = json.loads(node.get("modifiers"))
            modifiers['readonly'] = True
            node.set("modifiers", json.dumps(modifiers))
    res['arch'] = etree.tostring(doc)
return res

Regards

Avatar
Discard

I try this solution in V14 and not working

Best Answer
For that you can hide the edit button using below code

@api.model
def fields_view_get
(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(AccountMove, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,submenu=submenu)
if (YOUR condition):
doc = etree.fromstring(res['arch'])
if view_type=='form':
node = doc.xpath("//form")[0]
node.set("edit", "0")
res['arch'] = etree.tostring(doc)
return res


Avatar
Discard