Skip to Content
Menu
This question has been flagged
2 Replies
6160 Views
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?
Avatar
Discard
Best Answer

Hi FarFarAway,

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




I hope, you have understood all the codes :)
The above code is similar to adding read-only in each field in the form view but in a cleaner way.

Regards,

RPM IT SERVICES


Avatar
Discard
Author Best Answer

Oh God I need 8 karma just to reply to a answer on MY question ... facepalm

And can't seem to find a way to tag @RPM IT Services , this forum is incredibly bad made and this isn't the first stupid issue I had.


Anyway I'm replying to RPM IT Services, thank you for your answer but imo that is just more complicated, but more dynamic, way of just marking each field in the form as readonly manually. My goal is to have something simple and readable.


For now I've resulted in using readonly state field with locked/unlocked states and then generating 

states={'lost': [('readonly', True)]} for each model field.

Avatar
Discard