Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Conditional invisible field...

By
Carlos
on 5/4/16, 3:46 PM 665 views

In Odoo 9, i am using on_change function and i need to hide some custom fields in view sale order

in models.py i have:

...

#Selecction Field

b = fields.Selection([('hide', 'Hide'),('nohide', 'Not Hide'),('choiceoption', 'Choice Option')], default='choiceoption')

b_value = fields.Char('Value', default='Hello')

b_unit = fields.Char('Unit', readonly='True', default='World')

#Function

def on_change_b(self,cr,user,ids,b,b_value,b_unit,context=None):

if b=='choiceoption':

v = {'b_value': 'Choice Option', 'b_unit': 'Choice Option'}

return {'value': v}

elif b=='nohide':

v = {'b_value': 'No Hide Field b_value', 'b_unit': 'No Hide Field b_unit'}

return {'value': v}

else:

v = {'b_value': 'Yes Hide Field b_value', 'b_unit': 'Yes Hide Field b_unit'}

return {'value': v}

...

in templates.xml i have:

...

<field name="b" on_change="on_change_b(b, b_value, b_unit)" />

<field name="b_value" />

<field name="b_unit" />

...

1.- How i can hide the field "b_value" and "b_unit" when i choice "Hide" in the selection field, i think this must be writed in the function "on_change_b" but i do not know how doit the code, maybe with "attrs"


2.- How to initialize a field with invisible = yes for default like:

fieldxyz = fields.Char('This is Field XYZ', default=This is the value of fieldxyz', attrs="{'invisible':true}") <---This is correct?



3

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 5/5/16, 3:04 AM

Hi,

To use attrs you may try like this in your xml file:

<field name="b" />

<field name="b_value" attrs="{'invisible':[('b','==','hide')]}" />

<field name="b_unit" attrs="{'invisible':[('b','==','hide')]}"/>


To hide a field, you can use invisible="1" attribute in xml file:

for eg:

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


I tried to correct your code, so that you may understand the syntax of onchange in new api:

from openerp import models, fields, api
class SaleOrder(models.Model):    
    _inherit = 'sale.order'         
    b = fields.Selection([('hide', 'Hide'),('nohide', 'Not Hide'),('choiceoption', 'Choice Option')], default='choiceoption')    
    b_value = fields.Char('Value', default='Hello')    
    b_unit = fields.Char('Unit', readonly=True, default='World')         
    
    @api.onchange('b')

def _on_change(self):

         if self.b=='choiceoption':             

             self.b_value = 'Choice Option'

             self.b_unit = 'Choice Option'         

        elif self.b == 'nohide':             

             self.b_value = 'No Hide Field b_value'

             self.b_unit = 'No Hide Field b_unit'         

        else:

            self.b_value = 'Yes Hide Field b_value'

            self.b_unit = 'Yes Hide Field b_unit' 

Thank you Akhil, your code works for me :-)

Carlos
on 5/5/16, 11:25 AM
0

Odoo

--Odoo --
1328
| 5 2 5
kerala, India
--Odoo --

Working as a Odoo Functional and Technical Expert

Odoo
On 5/5/16, 1:25 AM

    Refer Onchange in Odoo9 New API

Your onchange syntax is not correct ,First chk that one




Thank you Dep for your advise, i will learn more about this.

Carlos
on 5/5/16, 11:26 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/4/16, 3:46 PM
Seen: 665 times
Last updated: 5/5/16, 11:21 AM