This question has been flagged
3 Replies
5141 Views

Hi,


I want to set the readonly attribute to true on a specific field but it remains editable


This is my code:


@api.model

    def fields_view_get(self, view_id=None, view_type='form', toolbar=False,

                        submenu=False):

        res = super(ResPartner, self).fields_view_get(

            view_id=view_id, view_type=view_type, toolbar=toolbar,

            submenu=submenu,

        )


        if view_type == 'form':

            user = self.env['res.users'].search([('id', '=', self.env.context.get('uid', False))])

            if not user[0].return_percentage_modify:

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

                res_fields = res_xml.xpath("//field[@name='porcentaje_devolucion']")

                if res_fields:

                    res_field = res_fields[0]

                    print('Entra res_field')

                    res_field.set('attrs',"{'readonly':True}")

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

        return res

Avatar
Discard
Best Answer

Hi,

This will set the field as readonly based on the condition.

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(ResPartner, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu,
)

if view_type == 'form':
user = self.env['res.users'].search([('id', '=', self.env.context.get('uid', False))])
if not user[0].return_percentage_modify:
doc = etree.XML(res['arch'])
for node in doc.xpath("//field[@name='porcentaje_devolucion']"):
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
Avatar
Discard
Best Answer

Hello Ivan,

you can write readonly=True into your field declaration


Thanks,

Ankit Vaghela

Avatar
Discard
Author Best Answer

But i want to make it readonly when the current user has the return_percentage_modify set to false on the database

Avatar
Discard