You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo V8中如何在向导打开时动态设置字段Domain

解决Odoo库存转移向导初始化时自动应用批次域的问题

1. 锁定needs_auth字段,禁止用户操作

先把needs_auth设为只读,用户就没法修改这个字段了:

needs_auth = fields.Boolean(related='product_id.needs_quality_auth', readonly=True)

要是不需要在界面显示这个字段,直接加invisible=True就行,或者去视图配置里设为不可见。

2. 给lot_id加动态域,初始化就生效

不用依赖onchange触发,直接在字段定义里写动态域,自动根据当前记录的product_id和needs_auth过滤批次:

lot_id = fields.Many2one(
    'stock.production.lot', 
    'Lot/Serial Number',
    domain="[('product_id', '=', product_id)] + ([('q_auth', '!=', False)] if needs_auth else [])"
)

逻辑很直接:先匹配对应产品的批次,如果needs_auth为真,再额外过滤已通过质检的批次。

3. 删掉冗余的onchange方法

现在动态域会自动生效,原来的_onchange_NQA_domain方法完全可以删掉,留着反而多余。

另一种方式:在视图里配置动态域

如果不想改动模型代码,也可以直接在视图里设置:

<!-- 给lot_id设置动态域 -->
<field name="lot_id" domain="[('product_id', '=', product_id), ('q_auth', '!=', False) if needs_auth else ()]"/>
<!-- 把needs_auth设为只读 -->
<field name="needs_auth" readonly="1"/>

这样修改后,打开库存转移详情向导的瞬间,每个条目都会自动应用对应的批次过滤规则,而且用户碰不了needs_auth字段。

内容的提问来源于stack exchange,提问作者odoo newbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:50:15