Odoo -根据字段不同使用不同的XPath



我希望Odoo根据条件显示Xpath或不显示Xpath

我有这三个字段

lots_id = fields.Many2one('stock.production.lot', 'Lot/Serial Number')
q_auth = fields.Boolean(related='lot_id.q_auth', string="Quality Auth.")
needs_auth= fields.Boolean("Needs Auth")

如果是needs_auth == False,我需要显示这个xpath

<xpath expr="//field[@name='lot_id']" position="replace">
<field name="q_auth" invisible="1"/>
<field name="lots_id" groups="stock.group_production_lot"
domain="[('product_id','=?', product_id)]"
context="{'product_id': product_id}"/>
</xpath>

但是如果是needs_auth == True,我需要这样的Xpath

<xpath expr="//field[@name='lot_id']" position="replace">
<field name="q_auth" invisible="1"/>
<field name="lots_id" groups="stock.group_production_lot"
domain="[('product_id','=?', product_id),('q_auth','!=',False)]"
context="{'product_id': product_id}"/>
</xpath>

您可以看到,唯一的区别是在域中。我不知道在XML中是否可以做到这一点,但如果不可能,我怎么用Python来做呢?

谢谢!

可以使用onchange函数,当needs_auth字段值发生变化时,返回lots_id字段域

例子:

@api.onchange("needs_auth")
def _update_lots_id_domain(self):
domain = [('product_id', '=?', self.product_id.id)]
if self.needs_auth:
domain.append(
('q_auth', '!=', False)
)
return {'domain': {'lots_id': domain}}

如何使用t-if/t-else:

<xpath expr="//field[@name='lot_id']" position="replace">
<field name="q_auth" invisible="1"/>
<t t-if="needs_auth">    
<field name="lots_id" groups="stock.group_production_lot"
domain="[('product_id','=?', product_id),('q_auth','!=',False)]"
context="{'product_id': product_id}"/>
</t>
<t t-else="">
<field name="lots_id" groups="stock.group_production_lot"
domain="[('product_id','=?', product_id)]"
context="{'product_id': product_id}"/>
</t>
</xpath>

最新更新