Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
11781 Переглядів

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().

Аватар
Відмінити

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

Найкраща відповідь

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

Аватар
Відмінити

I try this solution in V14 and not working

Найкраща відповідь
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


Аватар
Відмінити