Odoo 15中如何为lot_id字段设置域仅显示product_qty大于0的批次?
在Odoo 15中为stock.production.lot的lot_id字段设置筛选域
实现方案
根据需求,要让lot_id字段仅显示product_qty大于0的批次,有两种常用实现方式:
1. 字段层面全局设置(推荐)
如果是在自定义模块中定义该字段,或者需要所有视图都应用这个筛选规则,可直接在字段定义中添加domain参数:
from odoo import models, fields class YourCustomModel(models.Model): _name = 'your.custom.model' # 替换为实际模型名称 _description = 'Custom Model Description' lot_id = fields.Many2one( 'stock.production.lot', string="Item", domain="[('product_qty', '>', 0)]" )
如果是继承stock.production.lot模型修改已有字段:
from odoo import models, fields class StockProductionLotInherit(models.Model): _inherit = 'stock.production.lot' lot_id = fields.Many2one( 'stock.production.lot', string="Item", domain="[('product_qty', '>', 0)]" )
2. 视图层面局部设置
如果仅需要在特定视图中生效,可在对应的XML视图文件中为字段添加domain属性:
<record id="your_custom_form_view" model="ir.ui.view"> <field name="name">your.custom.model.form.view</field> <field name="model">your.custom.model</field> <field name="arch" type="xml"> <form> <!-- 其他视图元素 --> <field name="lot_id" domain="[('product_qty', '>', 0)]"/> <!-- 其他视图元素 --> </form> </field> </record>
关键说明
Odoo 15中stock.production.lot的product_qty是计算字段,但系统已为它定义了专属的search方法(_search_product_qty),因此可以直接在域中使用该字段进行筛选,无需额外处理计算字段的存储问题。
内容的提问来源于stack exchange,提问作者Ajmal C
相关产品推荐
相关产品推荐

