python - 如何在 Odoo 中渲染完成之前从 python 代码更新任何 View ?

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

我在 View 元素中传递颜色参数值时遇到问题。 所以我的模型有一个返回颜色的函数:

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

这样可以吗?

谢谢。

最佳答案

您可以使用fields_view_get方法从Python代码动态更新 View (在渲染 View 之前)。这只是我在 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

更新它

关于python - 如何在 Odoo 中渲染完成之前从 python 代码更新任何 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54550882/

相关文章:

python - Kivmob 不显示 android kivy 应用程序的广告横幅

python - 遍历字典值?

odoo - Openerp登录错误

python - 在python中获取工作表excel的数量

python - OpenERP : answering module configuration via XML-RPC

python - 删除后 SQLAlchemy 仍然能够从 session 中获取对象

python - 是否可以跨多行打破长函数名?

jquery - 无法更新(PUT)和删除(删除)django-piston 中的数据

Python 将字符串转换为带负数的 float 错误

Python:追加到一个字典值,如果已经存在,该值是一个列表,会添加到所有键的值而不是