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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM