Live Chat
True
FAQs
Odoo Security

Make entire form read only Odoo 14

So 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.

It is currently working but I'm looking for better solution.

Currently in my form I have to declare EACH field with:

field name="example" attrs="{'readonly':[('locked','=',True)]}"

Which is working but not really nice. I tried to lock the entire form:

group attrs="{'readonly':[('locked','=',True)]}"

notebook attrs="{'readonly':[('locked','=',True)]}"

which would be better than doing it on dozen of fields but it seems that it's not having effect.

Also I don't like that this is in form level, it should be at model level but I don't know any way of doing this except declaring:

state = fields.Selection([('locked', 'Locked'), ('unlocked', 'Unlocked')], 'State', default="unlocked")

and then each model field having kwarg:

states={'lost': [('readonly', True)]}

So my question:

Can I make model field readonly without relying on the state selection field? (I would prefer it to be a boolean).

If above isn't viable then is there a way to make entire form readonly?


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.