This question has been flagged
3 Replies
13689 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?

Avatar
Discard
Best Answer

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')]}"/>
Avatar
Discard
Author

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)]}"

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.

Author

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

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

Author

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?

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.

Author

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?

Best Answer

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

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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.

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

Author

But its not hiding.