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

How can I use 'state code' instead of 'state name' in res.partner form? (ie. OH vs. Ohio) v8

By
John Baldwin
on 8/15/14, 11:06 AM 633 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.

1

Prakash

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

OpenERP Devloper at SunARC Technology

Prakash
On 8/20/14, 6:40 AM

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/

 

0
Narayanamurthy
On 8/20/14, 9:33 AM

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

 

0
Mariusz Mizgier
On 8/15/14, 1:33 PM

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.

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.

John Baldwin
on 8/16/14, 5:45 AM

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.

Currenda Sp. z o.o., Mariusz Mizgier
on 8/16/14, 9:56 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

2 follower(s)

Stats

Asked: 8/15/14, 11:06 AM
Seen: 633 times
Last updated: 3/16/15, 8:10 AM