odoo12 通过python代码控制xml界面,更改字段属性(fields_view_get方法使用)

odoo12 通过python代码控制xml界面,更改字段属性(fields_view_get方法使用)

   @api.multi
    def get_required_module_list(self) -> list:
        """
        此方法用来设置,哪些模型可以将签字功能做成必输
        可继承修改,增加更多的模型签字必输
        """
        return [self._module]

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        """控制对应模型审批时签字字段的必输"""
        result = super(FrReimbursedApprovalWizard, self).fields_view_get(view_id, view_type, toolbar, submenu)
        required_module_list = self.get_required_module_list()
        if self._module in required_module_list:
            doc = etree.XML(result['arch'])
            for node in doc.xpath(r"//field[@name='fr_signature']"):
                node.set('required', '1')
                setup_modifiers(node, result['fields']['fr_signature'])
            result['arch'] = etree.tostring(doc, encoding='unicode')
        return result
from lxml import etree

from odoo.osv.orm import setup_modifiers

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        #override of fields_view_get in order to replace the name field to product template
        if context is None:
            context={}
        res = super(product_product, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        #check the current user in group_product_variant
        group_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'product', 'group_product_variant')[1]
        obj = self.pool.get('res.groups').browse(cr, uid, group_id, context=context)
        doc = etree.XML(res['arch'])
        if view_type == 'form':
            if obj and uid in [x.id for x in obj.users]:
                for node in doc.xpath("//field[@name='name']"):
                    node.set('invisible', '1')
                    node.set('required', '0')
                    setup_modifiers(node, res['fields']['name'])     #这行必须要有
                for node in doc.xpath("//label[@string='Product Name']"):
                    node.set('string','Product Template')
            else:
                for node in doc.xpath("//field[@name='product_tmpl_id']"):
                    node.set('required','0')
                    setup_modifiers(node, res['fields']['product_tmpl_id'])     #这行必须要有
                for node in doc.xpath("//field[@name='name']"):
                    node.set('invisible', '0')
                    setup_modifiers(node, res['fields']['name'])     #这行必须要有
                for node in doc.xpath("//label[@string='Product Template']"):
                    node.set('string','Product Name')
        res['arch'] = etree.tostring(doc)
        return res