conditions for a field to appear in odoo 14

I'd like field A to appear only if field B has a given value

field B doesn't belong to the same model, it belongs to a model connected through a many2one relationship

How do I write the contents of the attrs attribute for field A in the form ?

Until now I used

<field name="A"...
attrs="{'invisible': [('B', '=', 'some_value')

But until now both A and B belonged to the same model


Solution 1:

Field used in attrs must be present in view

You can't use dotted field names because Odoo will raise an error if the field name contain . when validating the view definition:

Invalid composed field %(definition)s in %(use)s (attrs.invisible ...)

There is an exception for list sub-views (One2many/Many2many display in a form view) where you can use parent.field_name

Solution 2:

ok, I reply to myself

First, in the model:

field_B = field.fieldtype(related='many2one_field.field_b')

Then, in the form view:

<field name="field_B" invisible="1"/>

and then

<field name="field_A" ...
        attrs="{'invisible': [('field_B', '=', 'some_value')]}">