是否可以在 Odoo 10 中动态更改选择字段的值?

Is it possible to change the value of a selection field dynamically in Odoo 10?

我希望我的选择取决于 Char 字段的值,例如,这样定义的 Char 字段:

my_char = fields.Char("Enter Something", readonly = False)

所以我想选择字段应该调用一个函数,比如“_get_value”

my_selection = fields.Selection(selection = ' _get_value')
@api.model
def _get_value(self):
    my_list = [('key1','value1')]
    #no idea how to assign the value of my_char to value1
    return my_list

最终,我希望下拉列表中的选择随着用户在 my_char 中输入不同的字符串而变化。
这在 Odoo 中可以实现吗?因为如果不是,我最好开始重组我的结构。非常感谢。

据我所知,字段类型 Selection 是不可能的。但是您可以为这种行为使用 Many2one 字段。

class MySelectionModel(model.Models):
    _name = "my.selection.model"

    name = fields.Char()

class MyModel(models.Model):
    _name = "my.model"

    my_char = fields.Char()
    my_selection_id = fields.Many2one(
        comodel_name="my.selection.model", string="My Selection")

    @api.onchange("my_char")
    def onchange_my_char(self):
        return {'domain': {'my_selection_id': [('name', 'ilike', self.my_char)]}}

或者没有 onchange 方法:

    my_selection_id = fields.Many2one(
        comodel_name="my.selection.model", string="My Selection",
        domain="[('name', 'ilike', my_char)]")

要让 Many2one 字段看起来像一个选择,请在表单视图中的该字段上添加 widget="selection"

域的外观应该由您决定。这里只是一个例子。

这里不用写方法。只需将字典声明为变量并在选择字段中调用它。

VAR_LIST = [('a','ABC'),
            ('p','PQR'),
            ('x','XYZ')]

my_selection = fields.Selection(string="Field Name",VAR_LIST)