How to hide field depend on condition odoo?

Astr0 picture Astr0 · Feb 3, 2015 · Viewed 8.6k times · Source

attrs="{'invisible': [('rule_id.type_test', '!=', 'A')]}" in code above not work, how i can fix this?

class test_list(models.Model):
    _name = 'test.list'

    type_test = fields.Selection([('A', 'aaaa'),('B','bbbbb')], default='A', string="Type", required=True)  
    rule_list = fields.One2many('test.rule', 'rule_id')

class test_rule(models.Model):
    _name = 'test.rule'

    rule_id = fields.Many2one('test.list', required=True)

    ul = fields.Many2one('product.ul', string='Package Logistic Unit'

<record model="ir.ui.view" id="rules_form_view">
            <field name="name">test.rules.form.view</field>
            <field name="model">test.rule</field>
            <field name="arch" type="xml">
                <form string="Test Rules">
                   <field name="ul" attrs="{'invisible': [('rule_id.type_test', '!=', 'A')]}"/> 
                </form>
            </field>
</record>

Answer

Ludwik Trammer picture Ludwik Trammer · Feb 3, 2015

The fields you use in attrs need to be present in the current view.

You can add this field to model:

class test_rule(models.Model):
    _name = 'test.rule'

    rule_id = fields.Many2one('test.list', required=True)
    type_test = fields.Selection(related='rule_id.type_test')
    ul = fields.Many2one('product.ul', string='Package Logistic Unit')

And then to your form:

<form string="Test Rules">
    <field name="type_test" invisible="1"/>
    <field name="ul" attrs="{'invisible': [('type_test', '!=', 'A')]}"/> 
</form>