Search code examples
odooodoo-17

Odoo 17 - Conditionally hide a field in parent view


Using Odoo 17, I've extended the res.partner model with some custom fields:

class ResPartner(models.Model):
    _inherit = 'res.partner'
    is_farmer = fields.Bool()
    ...

I'd like to conditionally hide the Tax ID field, based on if the is_farmer field is True or False.

enter image description here

I know I can hide the field using the position="attributes" syntax, as follows:

<record id="view_res_partner_form_inherit" model="ir.ui.view">
    <field name="name">res.partner.form.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">

        <xpath expr="//field[@name='vat']" position="attributes">
            <attribute name="invisible">1</attribute>
        </xpath>

    </field>
</record>

And I know I can control the visibility of a field individually using a Python expression in the invisible attribute:

<field name="vat" invisible="is_farmer"/>

But I can't seem to figure out how to combine this syntax to conditionally control the visibility of a field in an inherited view.

I've spent some time discussing with chatgpt, who suggests I should create a custom OWL component, but that really seems like overkill. Is this operation possible with the standard views?


Solution

  • Well this was easier than I thought in the end. I just needed to pass the expression as the value of the attribute node.

    <xpath expr="//field[@name='vat']" position="attributes">
        <attribute name="invisible">is_farmer</attribute>
    </xpath>