【问题标题】:How to update any view from python code, before rendering completion, in Odoo?如何在 Odoo 中渲染完成之前从 python 代码更新任何视图?
【发布时间】:2019-02-06 10:03:06
【问题描述】:

我在视图元素中为颜色参数传递值时遇到问题。 所以我有我的模型,它具有返回颜色的功能:

class MyTask(models.Model):
     _inherit = "project.task"
     is_special=fields.Boolean()     

     @api.model
     def get_colors(self):
          return 'red: is_special == true;'

我也有这样的看法:

<record id="my_module_timeline" model="ir.ui.view">
<field name="model">project.task</field>
<field name="type">timeline</field>
<field name="arch" type="xml">
    <timeline date_start="date_start"
            date_stop="date_end"
            default_group_by="project_id"
            event_open_popup="true"
            colors= <-- how can i get the value from my model get_colors() function?
            >
    </timeline>
</field>

colors 参数必须是字符串,不能是模型字段。 我尝试了很多选项来从模型函数中获取这个字符串,但没有好的结果。

&lt;timeline&gt; 元素只是示例,它也可以是树、日历等。 对于测试我从:

https://github.com/OCA/web/tree/11.0/web_timeline

这样可以吗?

谢谢。

【问题讨论】:

    标签: python python-3.x odoo odoo-10 odoo-11


    【解决方案1】:

    您可以使用fields_view_get 方法从python 代码动态更新视图(在呈现视图之前)。这只是我在 Odoo 中找到的一个示例:

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(MailThread, self).fields_view_get(
            view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu
        )
        if view_type == 'form':
            doc = etree.XML(res['arch'])
            for node in doc.xpath("//field[@name='message_ids']"):
                # the 'Log a note' button is employee only
                options = safe_eval(node.get('options', '{}'))
                is_employee = self.env.user.has_group('base.group_user')
                options['display_log_button'] = is_employee
                # save options on the node
                node.set('options', repr(options))
            res['arch'] = etree.tostring(doc, encoding='unicode')
        return res
    

    将其放入您的模型中。使用doc.xpath 查找节点并使用node.set 更新它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2023-01-30
      相关资源
      最近更新 更多