Live Chat
True
FAQs
Odoo Security

Make entire form read only Odoo 14

I have a model with few fields and one of them is boolean representing locked state. If it's locked then fields can't be edited in the forms and vice-versa. 


You need to use of fields_view_get() method which is basically loaded every time when the view is loaded.
The thing to be careful when using this method is that we can not get any data related to the respective record inside this method as it is called when loading the view (at that time, the data of the record is not loaded).
Please check the below code:
#Don't forget to import these before executing this code.
from lxml import etree
import simplejson #If not installed, you have to install it by executing pip install simplejson
@api.model
def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False):
res = super(TestModel, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu).
if self.env.context.get('make_form_readonly'): # Check for context value
doc = etree.XML(res['arch']) #Get the view architecture of record
if view_type == 'form': # Applies only if it is form view
for node in doc.xpath("//field"): #Get all the fields navigating through xpath
modifiers = simplejson.loads(node.get("modifiers")) #Get all the existing modifiers of each field
modifiers['readonly'] = True #Add readonly=True attribute in modifier for each field
node.set('modifiers', simplejson.dumps(modifiers)) #Now, set the newly added modifiers to the field
res['arch'] = etree.tostring(doc) #Update the view architecture of record with new architecture
return res



Was this article helpful?

FAQ HOME

To install this Web App in your iPhone/iPad press and then Add to Home Screen.