This question has been flagged
3 Replies
4946 Views

I would like to use state code (2 letters) as a searchable drop down in res partner address form, or all addresses for that matter.  I'm using the latest version 8 and thought this was a config option but can't find it.

Avatar
Discard
Best Answer

In Custom Module Inherit res.country.state object and Override name_get method and shows State Code based on your requirement.

Example:-

class CountryState(osv.osv):
    _inherit = "res.country.state"
    
    def name_get(self, cr, uid, ids, context=None):
        if not len(ids):
            return []
        res = []
        reads = self.read(cr, uid, ids, ['code'], context=context)
        for record in reads:
            code = record['code']
            res.append((record['id'], code))        
        return res

see the name_get and name_search ORM method https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/methods/

 

Avatar
Discard
Best Answer

It is working already as you say - try it on your own - when you type the right code, a dropdown menu selection with state representing code you've typed appears.

Avatar
Discard
Author

I understand this. There is a drop down to select the "Full" name of the state. I would like to store just the two letter state code here. Where is the code that imports the list for the drop down? That is what I need to change.

Instead of changing whole search in Odoo, it would be easier for you to create a new model. which will store values as you wish - many2one search is based on name column, so you would have to create a model, which in name stores code, and in code stores name.

Best Answer

Pass context value in xml...

<field name='state' context="{'code':True}"

 

In py file

class res_country_state(osv.osv):
    _inherit = "res.country.state"
    
    def name_get(self, cr, uid, ids, context=None):
        if not len(ids):
            return []

        if not context:

              context={}
        res = []

        res=super(res_country_state,self).name_get(cr, uid, ids)

        state_code=context.get('code',False)
        reads = self.read(cr, uid, ids, ['code'], context=context)

        if state_code:

            res = []
            for record in reads:
                 code = record['code']
                 res.append((record['id'], code))        
        return res

 

Avatar
Discard