Home > other >  Odoo - Different XPath depending on a field
Odoo - Different XPath depending on a field

Time:11-29

I want Odoo to show an Xpath or not, depending on a condition

I have this 3 fields

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")

If needs_auth == False, i need to show this 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>

but if needs_auth == True I need the Xpath to be like this

 <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>

You can see that the only difference is in the domain. I don't know if this is possible to do it in XML, but in case is not possible, how can I do it with Python?

Thanks!

CodePudding user response:

You can use onchange function and return lots_id field domain when the needs_auth field value change.

Example:

@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}}

CodePudding user response:

How about using 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>
  • Related