This question has been flagged
3 Replies
22024 Views

I know that we can make a complete form by placing the read-only attribute on each field. But my requirement is to make the complete form read-only based on status (As I have too many fields in my form view).

Avatar
Discard
Best Answer

You can set all fields to readony, using a context value and overriding the fields_view_get() method of your model

Example:

If you want to turn the entire account.invoice form to readonly, you can use the next code, using a context value:

Calling from any specific button, menu or field set a context value. I choose 'turn_view_readonly' value to True
from a the sale order view xml button.

<button name="action_view_invoice" string="View Invoice" type="object" class="oe_highlight"        
context="{'turn_view_readonly':True}"/>


And  inherit  account.invoice code model, checking for this value

from odoo import models, api
from lxml import etree
import simplejson

class AccountInvoice(models.Model):
_inherit = 'account.invoice'
@api.model
def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False):
context = self._context
res = super(AccountInvoice, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)

if context.get('turn_view_readonly'): # Check for context value
doc = etree.XML(res['arch'])
if view_type == 'form': # Applies only for form view
for node in doc.xpath("//field"): # All the view fields to readonly
node.set('readonly', '1')
node.set('modifiers', simplejson.dumps({"readonly": True}))

res['arch'] = etree.tostring(doc)
return res

You can also look for 'active_id' or 'active_ids' value in context, and check the state field of the recordset and turn the fields to readonly..

if context.get('active_id') and self.browse(context.get('active_id')).state == 'X':
doc = etree.XML(res['arch'])
# ... etc

  

Avatar
Discard

fields_view_get
This method only gets call when page get refresh, not when you open any record.

Best Answer

You can make a boolean field and make a function to turn it true if status achieved then make view read only if that field is true

Avatar
Discard
Best Answer

Hi,

Can try to add security rule, disable rights to create and edit, grant only read access. 

Avatar
Discard
Author

Dear Sushma. Thank you for your answer. But this is not I'm looking at. I need to make readonly in a specific status for all groups.

Hi Venu,

Indeed, but this is only way you can achieve, with group tag readonly won't work. This need to be added only to fields.

Thanks !