Odoo Help


Display field when the change of many2one field

on 4/5/14, 10:34 AM 3,623 views

I have one many2one field. Many2one field is country_id(res.country).

When i select India in the many2one field, two char field should be displayed. otherwise fields should be hidden.

When select another country from the many2one field another two char field should be displayed?

how i can do this?



| 5 4 7
Bikaner, India

OpenERP Devloper at SunARC Technology

On 4/7/14, 6:00 AM

The below code When you select India in the many2one field, two char field should be display. Python Code:-

_columns = {        
    'country_id': fields.many2one('res.country', 'Country'),
    'country_name': fields.related('country_id', 'name', type="char", relation="res.country", string="Name"),
        'field1_name': fields.char('Field1', size=64),  
        'field2_name': fields.char('Field2', size=64),  

def onchange_country(self, cr, uid, ids, country_id, context=None):
value = {}
res_country = self.pool.get('res.country')        
if country_id:           
    value = {
      'country_name': res_country.browse(cr, uid, country_id).name
return {'value': value}

XML code:-

<field name="country_id" on_change="onchange_country(country_id)"/> 
<field name="country_name" invisible="1"/> 
<field name="field1_name" attrs="{'invisible':[('country_name', '!=', 'India')]}"/>
<field name="field2_name" attrs="{'invisible':[('country_name', '!=', 'India')]}"/>

Thank you very much,Prakash. I just ask you a doubt. If i directly give country_id=23 for india will it work.ie, attrs="{'invisible':[('country_id', '!=', 23)]}"

on 4/7/14, 6:04 AM

Yes it will works. But country_id hard coded. For Example in my DB Country ID for India is stored 105. so need to change code ID based on DB. The above code will works in any DB.

on 4/7/14, 6:14 AM

But i give the above attr line, its not working.

on 4/7/14, 7:44 AM

Have u used the same code?.. on_change function and invisible country_name in the form view.

on 4/7/14, 8:07 AM

sorry, not your code Prakash. I just simply created a field with <field name="field1_name" attrs="{'invisible':[('country_id', '!=', '2')]}"/> , but its not working. will it not work without python code?

on 4/7/14, 8:57 AM

Remove the symbol ' ' and apply <field name="field1_name" attrs="{'invisible':[('country_id', '!=', 2)]}"/> And to check ID using select id from res_company where name="India' and apply the ID in xml file.

on 4/7/14, 10:48 AM

Hi Prakash, I'm asking another doubt: I want to display first login time in my attendance custom report. Did you know the code? One employee will login more than one time a day, but i want the first login time. http://help.openerp.com/question/48949/generating-a-custom-report-for-employees-login-time/ Can you please help me?

on 4/8/14, 5:55 AM
On 4/7/14, 11:46 AM

You only need a conditition like that, that will hide field if country is not india

<field name="your_field_name_india" attrs="{'invisible':[('country_id', '!=', ref('base.in'))]}"/>
<field name="your_field_name" attrs="{'invisible':[('country_id', '=', ref('base.in'))]}"/>

*base.id is the XML_ID for the country INDIA

On 4/7/14, 1:45 AM

create a hidden related field to many2one . based on that hidden field change the attrs::invisible on the fields that you want to display at runtime.

I didn't get. I already have a many2one field that is not hidden. I already tried attrs:invisible, but it is not working. i think my code is wrong. can you please wrote the condition here.

on 4/7/14, 1:49 AM

attrs="{'invisible':[(field , '=', 'india')]}"

on 4/7/14, 2:23 AM

But its not hiding.

on 4/7/14, 2:41 AM

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 4/5/14, 10:34 AM
Seen: 3623 times
Last updated: 4/5/16, 3:33 AM