Skip to Content
Menu
This question has been flagged
1 Reply
6389 Views

Hi everone,

I know the method to make the whole form view read-only always when the method is runned

but I need to insert a condition:

@api.model

def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False):

    res = super(SaleOrder, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)

    #we can not get any data related to the respective record

    #so I can not insert the following condition:

    # if self.status_invoice == 'invoiced':

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

    if view_type == 'form':

        for node in doc.xpath("//field"):

            modifiers = simplejson.loads(node.get("modifiers"))

            modifiers['readonly'] = True 

            node.set('modifiers', simplejson.dumps(modifiers)) 

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

    return res 


is there any way to extend 'attrs' via python?

... attrs['readonly'] += ('|',('invoice_status','=','invoiced'))

Avatar
Discard
Author Best Answer

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

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

        if view_type == 'form':

            for node in doc.xpath("//field"):

                modifiers = simplejson.loads(node.get("modifiers"))

                if 'readonly' not in modifiers:

                    modifiers['readonly'] = [['invoice_status','=','invoiced']]

                else:

                    if type(modifiers['readonly']) != bool:

                        modifiers['readonly'].insert(0, '|')

                        modifiers['readonly'] += [['invoice_status','=','invoiced']]

                node.set('modifiers', simplejson.dumps(modifiers)) 

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

        return re

Avatar
Discard
Related Posts Replies Views Activity
0
Dec 24
3
0
Dec 24
3
2
Oct 24
2434
2
Jun 24
465
2
Dec 23
11991