NeoVe NeoVe - 24 days ago 7
Python Question

Boolean field not working - Odoo v9 community

I want to hide/show fields, depending on a boolean field, in Odoo v9 community

First, I don't know why it isn't working, this is my code:

class account_invoice(models.Model):
_inherit = "account.invoice"

is_export = fields.Boolean(string="¿Es exportación?")
field_1 = fields.Char ...
field_2 = fields.Many2one ...


Now on my view:

<record model="ir.ui.view" id="embarque_form_view">
<field name="name">account.invoice.embarque.form</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form" />
<field name="arch" type="xml">
<field name='partner_id' position="after">
<field name="is_export" class="oe_edit_only" widget="radio"/>
<field name="puerto_embarque" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="puerto_desembarque" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="moneda_export" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="tara_unit" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="weight_unit" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="net_weight_unit" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="transporte_tipo" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
<field name="modal_idad" attrs="{'invisible': [('is_export', '!=', 'True')]}"/>
</field>
</field>
</record>


The fields are actually hidden, but when I try to click on the boolean field, to activate them, it doesn't work, I mean it just clicks and doesn't becomes 'True' or anything, it remains like if it was unclicked.

I don't know if this is about the attrs I'm adding to every other field, like:
attrs="{'invisible': [('is_export', '!=', 'True')]}"


What I'm I doing wrong here?

Answer

Declare is_export field with default value False

Try with following code:

is_export = fields.Boolean(string="¿Es exportación?", default=False)

Update your view file with

<record model="ir.ui.view" id="embarque_form_view">
    <field name="name">account.invoice.embarque.form</field>
    <field name="model">account.invoice</field>
    <field name="inherit_id" ref="account.invoice_form" />
    <field name="arch" type="xml">
        <field name='partner_id' position="after">
            <field name="is_export"/>
            <field name="puerto_embarque" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="puerto_desembarque" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="moneda_export" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="tara_unit" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="weight_unit" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="net_weight_unit" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="transporte_tipo" attrs="{'invisible': [('is_export', '=', False)]}"/>
            <field name="modal_idad" attrs="{'invisible': [('is_export', '=', False)]}"/>
        </field>
    </field>
</record>

Restart Odoo server and upgrade your module.