要求:
一个按钮显示用户登录的名字 和 隐藏一个字段。用重载的方法
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super().fields_view_get( view_id=view_id, view_type=view_type,
toolbar=toolbar, submenu=submenu)
if res['type'] == "form":
name = self.env.user.name
doc = etree.XML(res['arch'])
nodes = doc.xpath("//button[@name='btn_user_name']")
for node in nodes:
# 设置context
node.set("string",name)
node.set("context","{'contexttest':'%s'}" % (name,) )
for node in doc.xpath("//field[@name='employ_ids']"):
node.set('invisible', '1' )
orm.setup_modifiers(node, res['fields']['employ_ids'])#这行必须要有
res['arch'] = etree.tostring(doc)
return res
关键点 在 orm.setup_modifiers(node, res['fields']['employ_ids'])
可以看下 这个代码
def setup_modifiers(node, field=None, context=None, in_tree_view=False):
""" Processes node attributes and field descriptors to generate
the ``modifiers`` node attribute and set it on the provided node.
Alters its first argument in-place.
:param node: ``field`` node from an OpenERP view
:type node: lxml.etree._Element
:param dict field: field descriptor corresponding to the provided node
:param dict context: execution context used to evaluate node attributes
:param bool in_tree_view: triggers the ``column_invisible`` code
path (separate from ``invisible``): in
tree view there are two levels of
invisibility, cell content (a column is
present but the cell itself is not
displayed) with ``invisible`` and column
invisibility (the whole column is
hidden) with ``column_invisible``.
:returns: nothing
"""
modifiers = {}
if field is not None:
transfer_field_to_modifiers(field, modifiers)
transfer_node_to_modifiers(
node, modifiers, context=context, in_tree_view=in_tree_view)
transfer_modifiers_to_node(modifiers, node)
根据上一篇最后 的两个 属性 设置和 json 到 arch 里面应该很容易理解了