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.

3

Display field when the change of many2one field

By
Remya
on 4/5/14, 10:34 AM 3,279 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?

1

Prakash

--Prakash--
3173
| 5 4 7
Bikaner, India
--Prakash--

OpenERP Devloper at SunARC Technology

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

Remya
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.

Prakash
on 4/7/14, 6:14 AM

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

Remya
on 4/7/14, 7:44 AM

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

Prakash
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?

Remya
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.

Prakash
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?

Remya
on 4/8/14, 5:55 AM
1
Serge
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

0
silvi
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.

Remya
on 4/7/14, 1:49 AM

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

silvi
on 4/7/14, 2:23 AM

But its not hiding.

Remya
on 4/7/14, 2:41 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

0 follower(s)

Stats

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