如何使用全局变量更新 Odoo 10 的选择字段?

How to use a global variable to update a selection field with Odoo 10?

您好,是否可以有一个基于 'onchange' 更新的全局变量并将其分配给选择字段?

variable = []

class a:
    @api.onchange('some_other_filed')
    def function1(self):
        global variable
        variable = #something based on some other filed

    test = fields.Selection(variable)

这个onchange会反映在选择字段中吗?我得到的只是选择字段中的空列表。

不推荐使用全局变量。如果您想让 Odoo 中的任何地方始终可用,您可以在 ir.config_parameter table 上使用持久字段。您可以在 设置 > 技术 > 参数 > 系统参数.

中查看存储的配置参数

您可以这样设置参数的值:

self.env['ir.config_parameter'].set_param('use_journal_setting', True)

并像这样获取其他值:

location = self.env['ir.config_parameter'].get_param(
    'ir_attachment.location'
)

您也可以直接用一些值更新选择字段,或者您可以使用参数更新它。

@api.onchange('field_name')
def onchange_field_name(self):
    self.test = self.env['ir.config_parameter'].get_param('parameter_name')

注意:如果您需要很多这样的全局变量,请考虑创建您自己的参数table。