odoo 的加载流程 四、load_views 流程分析实例

要求:

一个按钮显示用户登录的名字 和 隐藏一个字段。用重载的方法

@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 里面应该很容易理解了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值