首页 > 代码库 > openerp创建动态视图-fields_view_get

openerp创建动态视图-fields_view_get

openerp的视图结构是以XML的格式存放于ir.ui.view表中,属于静态格式,设计之后就固定,

但可以通过在model中重写fields_view_get函数,在视图加载时修改arch属性,动态修改视图的结构

def fields_view_get(self, cr, uid, view_id=None,view_type=form,context=None,toolbar=False,submenu=False):        ip_obj = self.pool.get(hr.rule.input)        res = super(hr_inputs_editor,self).fields_view_get(cr,uid,view_id,view_type,context,toolbar,submenu)        if view_type==form:            treev = res[fields][line_ids][views][tree]            doc = etree.XML(treev[arch])            for node in doc.xpath("/tree/field"):                name = node.get(name,False)                if name.startswith(value):                    name = name.replace(value,input) + _code                    cd = context.has_key(name) and context[name] or False                    if cd:                        ip_ids = ip_obj.search(cr,uid,[(code,=,cd)],limit=1,context=context)                        if ip_ids:                            text = ip_obj.read(cr,uid,ip_ids,[name])[0][name]                            node.set(string,text)                    else:                        node.set(modifiers,{"tree_invisible":true})            treev[arch] = etree.tostring(doc)        return res

注意,必须是通过etree将res[‘arch‘]加载到DOM中,通过xpath查找并set修改属性,然后将修改后的dom通过etree.tostring(doc)写回res[‘arch‘]

如果要修改一对多字段下的子视图时,需要先运算到子视图,如:

treev = res[fields][line_ids][views][tree]