Skip to main content
 首页 » 编程设计

python之Odoo:One2many 字段不工作

2025年05月04日14wayfarer

我在 Odoo 中遇到了 one2many 字段的奇怪行为。

这是我的代码:

模型.py:

class mrp_bom_inherit(models.Model):  
    _inherit = 'mrp.bom' 
    producten_tussenmodel = fields.One2many(comodel_name='tussenmodel_wc_producten', inverse_name='bom_id', string="producten", copy=True) 
 
class tussenmodel_wc_producten(models.Model): 
    bom_id = fields.Integer() 
    routing_id = fields.Integer() 
    producten = fields.Char(string="Productnr.") 

View .xml:

<field name="producten_tussenmodel" widget="one2many_list" nolabel="1"> 
    <tree string="Een Naar Veel" editable="bottom"> 
        <field name="producten" domain="[('routing_id', '=', 32)]"/> 
    </tree> 
</field>  

这给出了输出:

但它应该只显示“bbb”,因为“bbb”是数据库中表 tussenmodel_wc_producten 中的 routing_id = 32 和表 tussenmodel_wc_producten 中的 bom_id 等于 mrp.bom 中的 id 的唯一记录。

我已经通过 SQL 查询检查了这一点:

select producten 
from mrp_bom as m JOIN tussenmodel_wc_producten as t ON(m.id = t.bom_id)  
where t.routing_id = 32 

它只有输出“bbb”。

我在这里做错了什么?

编辑: Ludwik Trammer 的更多截图:

请您参考如下方法:

字段上的

domain 属性仅控制哪些对象出现在可用对象列表中(即可以可能选择的对象关系)当用户编辑表单时。它不会做更多的事情,特别是它不会控制关系中实际存在的对象。

如果对象已经与“aaa”、“bbb”、“ccc”和“ddd”有关系,设置域不会改变它。