Shravy
Shravy

Reputation: 666

How to hide a column in one2many tree view dynamically?

I wanted to hide a field in one2many treeview based on a boolean field,

Boolean field

If the boolean field is checked, then the field total quantity to be displayed in the following tree view..

Tree view

The tree view is of class'account.invoice.line' and the code is as follows(the boolean field):

class account_invoice_line(models.Model):
    _name = "account.invoice.line"
    _description = "Invoice Line"
    _order = "invoice_id,sequence,id"

    name = fields.Text(string='Description')
    tot_qty_bool = fields.Boolean(string='Total Quantity', default=False)
    tot_qty = fields.Float(string='Total Quantity')
    origin = fields.Char(string='Source Document',
        help="Reference of the document that produced this invoice.")
    sequence = fields.Integer(string='Sequence', default=10,
        help="Gives the sequence of this line when displaying the 
invoice.")
    invoice_id = fields.Many2one('account.invoice', string='Invoice 
Reference',
        ondelete='cascade', index=True)
    uos_id = fields.Many2one('product.uom', string='Unit of Measure',
        ondelete='set null', index=True)
    product_id = fields.Many2one('product.product', string='Product',
        ondelete='set null', index=True)
    account_id = fields.Many2one('account.account', string='Account',
        domain=[('type', 'not in', ['view', 'closed'])],
        default=_default_account,
        help="The income or expense account related to the selected 
product.")
    price_unit = fields.Float(string='Unit Price', required=True,
        digits= dp.get_precision('Product Price'),
        default=_default_price_unit)
    price_subtotal = fields.Float(string='Amount', digits= 
dp.get_precision('Account'),
        store=True, readonly=True, compute='_compute_price')
    quantity = fields.Float(string='Actual Quantity', digits= 
dp.get_precision('Product Unit of Measure'),
        required=True, default=1)
    discount = fields.Float(string='Discount (%)', digits= 
dp.get_precision('Discount'),
        default=0.0)
    invoice_line_tax_id = fields.Many2many('account.tax',
        'account_invoice_line_tax', 'invoice_line_id', 'tax_id',
        string='Taxes', domain=[('parent_id', '=', False)])
    account_analytic_id = fields.Many2one('account.analytic.account',
        string='Analytic Account')
    company_id = fields.Many2one('res.company', string='Company',
        related='invoice_id.company_id', store=True, readonly=True)
    partner_id = fields.Many2one('res.partner', string='Partner',
        related='invoice_id.partner_id', store=True, readonly=True)

and the code part I had tried something like this in fields_view_get(which was not successful)

def fields_view_get(self, view_id=None, view_type='form', 
toolbar=False, submenu=False):
        res = super(account_invoice_line, self).fields_view_get(
            view_id=view_id, view_type=view_type, toolbar=toolbar, 
submenu=submenu)
        if self._context.get('tot_qty_bool'):
            doc1 = etree.XML(res['arch'])
            for node in doc.xpath("//field[@name='tot_qty']"):
                if self._context['tot_qty_bool'] in ('False'):
                    node.set('invisible', '1')
                else:
                    node.set('invisible', '0')
            res['arch'] = etree.tostring(doc)
        return res

Please guide me on the correct way of doing this or let me know if there is any other alternative correct way of achieving the same. Would be really a great help.

Upvotes: 1

Views: 4325

Answers (2)

Kenly
Kenly

Reputation: 26768

fields_view_get is called when odoo is rendering a view of the model and only during the initial rendering.So what you try to do can only be done when an account.invoice.line view is rendered.

Try to inherit invoice_form view and use attrs to hide tot_qty field. (you need to add tot_qty_bool field inside the tree view).

<record id="invoice_form_inherit" model="ir.ui.view">
  <field name="name">invoice.form.inherit</field>
  <field name="model">account.invoice</field>
  <field name="inherit_id" ref="account.invoice_form"/>
  <field name="arch" type="xml">
      <xpath expr="//tree/field[@name='name']" position="after">
            <field name='tot_qty_bool' invisible="True"/> 
            <field name='tot_qty' attrs="{'invisible':[('tot_qty_bool', '=', False)]}"/>
        </xpath>

  </field>
</record>

Upvotes: 3

CZoellner
CZoellner

Reputation: 14801

As @macdelacruz already has mentioned in a comment, use attrs and invisible in your tree view definition.

I've tested an similar example on Odoo V8 runbot.

From origin timesheet form view:

<field name="name" />

Just change it to:

<field name="name" attrs="{'invisible': [('unit_amount','=',0)]}"/>

Save it, reload and try to play around with it. It's working.

Upvotes: 1

Related Questions