在渲染完成之前,如何在Odoo中更新python代码中的任何视图?

问题描述 投票:2回答:1

我在view元素中传递colors参数的值有问题。所以我的模型具有返回颜色的功能:

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参数必须是字符串,并且它不能是模型的字段。我尝试了很多选项来从模型函数中获取此字符串,但没有很好的结果。

<timeline>元素只是一个例子,它也可以是树,日历等。对于测试我得到它:

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

有可能这样吗?

谢谢。

python python-3.x odoo odoo-10 odoo-11
1个回答
2
投票

您可以使用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更新它

© www.soinside.com 2019 - 2024. All rights reserved.