Help

5

how can i hide a field in odoo9

Avatar
Sreejith P

i would like to create a module which disable a field discount in account.invoice.line if a boolean field discount_bool is set to false in account.invoice. when i tried below mentioned code i got an error RuntimeError: maximum recursion depth exceeded while calling a Python object

<record id="discount_invoice_lines_ids" model="ir.ui.view">           

<field name="model">account.invoice</field>           

<field name="name">discount_invoice_lines</field>           

<field name="inherit_id" ref="account.invoice_form"/>           

<field name="arch" type="xml">               

<xpath expr="//field[@name='invoice_line_ids']/tree/field[@name='discount']" position="replace">                   

<field name="discount" attrs="{'invisible':[('hide_discount', '=', False)]}"/>               

</xpath>           

</field>       

</record>
in my .py

class DiscountField(models.Model):
 _inherit = 'account.invoice.line'

record = fields.Many2one('account.invoice')

discount = fields.Float(string='Discount (%)')

hide_dis = fields.Boolean(related='record.discount_bool')

7 Comments
Avatar
Discard
Avatar
Artem
-

record = fields.Many2one('account.invoice') - already exists as 'invoice_id'

<field name="discount" attrs="{'invisible':[('hide_discount', '=', False)]}"/> I do not see where 'hide_discount' is.

Avatar
Artem
-

An invoice head and Invoice lines are all in the same view. So I think you need not any modification in 'account.invoice.line'. Add 'hide_discount' field to 'account.invoice' and modify the 'account.invoice_form',

Avatar
Mai Ecarde
-

For starters I think you have a typo:

On your view the invisible attrs logic uses "hide_discount"

but your related field is only "hide_dis"

Avatar
Mai Ecarde
-

You will also need to add the field "hide_dis" to the tree. Add the following before the discount field:

<field name="hide_dis" invisible="True"/>

Also note that if this works the column will still be visible on the tree (only the values are hidden)

Avatar
Sreejith P
-

sorry its a typo.

@Artem the field is hide_discount and is used to take the value from account.invoice but it worked with invoice_id,but the column id still there and but values are hidden. By adding `hide_discount` field to 'account.invoice' how can i create the view in `account.invoice.line`

Avatar
Sreejith P
-

@Mai Ecarde you are right the column will still be visible on the tree (only the values are hidden). How can i hide the column when discount_bool is false in `account.invoice`

Avatar
Mai Ecarde
-

You could try having the field 'invoice_line_ids' defined twice in the view with opposite visibility on hide discount. One with the discount column and one without. I think this might work due to only one instance of the field being visible.... although I tend to get bad results from having a field in a view twice. If this doesn't work I am not sure there is a simple way.

1 Answer
1
Avatar
Jesni Banu
Best Answer

You can hide discount field as follow.

<xpath expr="//field[@name='invoice_line_ids']/tree/field[@name='discount'] position="attributes">                  
     <attribute name="attrs">{'invisible': [('hide_discount', '=', False')]}</attribute>              
</xpath>

Avatar
Discard