如何从Odoo服务器操作

时间:2016-12-15 03:17:45

标签: openerp

我正在尝试通过Odoo UI创建服务器操作,该操作将改变视图中另一个字段的域。在处理Odoo源代码时,这似乎是一个非常常见的用例,如以下文档中所示:

https://www.odoo.com/documentation/10.0/reference/orm.html#odoo.api.onchange

在这些文档中,它们表明如果我在模型的源代码中,我可以定义一个onchange方法并返回一个域,例如,我试图在{ {1}}模型将是:

sale.order.line

换句话说,当销售订单行的产品发生变化时,请更新route_id字段中的可用选项。

有没有办法通过UI创建的服务器操作完成同样的事情?我无法弄清楚如何从Python代码中返回一个域。

代码部分中的注释说:

@api.onchange('product_id')
def _onchange_product(self):
    return {
        'domain': {'route_id': [('id', 'in', x_all_route_ids.ids)]}
    }

我看不出如何使用它来返回域名。有人有任何想法吗?

我尝试将python代码字段设置为:

# Available variables:
#  - time, datetime, dateutil, timezone: Python libraries
#  - env: Odoo Environement
#  - model: Model of the record on which the action is triggered
#  - record: Record on which the action is triggered if there is one, otherwise None
#  - records: Records on which the action is triggered if there is one, otherwise None
#  - log : log(message), function to log debug information in logging table
#  - Warning: Warning Exception to use with raise
# To return an action, assign: action = {...}

但这不起作用。 route_id列表保持不变。

2 个答案:

答案 0 :(得分:1)

我从Odoo技术支持中获得了一些见解,结果证明这是可能的。分配给action变量的任何内容都将被视为服务器操作的返回值。

所以你可以这样做:

action = {
    'domain': {
        'route_id': [('id', 'in', record.x_all_route_ids.ids)]
    }
}

答案 1 :(得分:0)

这是不可能的。只有这些特殊的on-change方法返回值才能正确评估。