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

get country_name of sale order's partner (display country in the sales orders' tree view)

By
Yao
on 11/29/13, 12:05 AM 2,028 views

I need retrieve the country name of the partner to display them in the sales quotations/orders in tree view.

  1. I tried to use field but i received server error. The code is shown below:

    'sale_country':fields.related('partner_id','country_id',type="char", readony=True),

  2. I tried to write a function to retrieve the name. It seems that I can get the name(by printing the country_name, I can see that the data is retrieved), but the system still throw me exception/error. Please find my code below and the error message below.

Code:

def get_country(self, cr, uid, ids, name, arg, context=None):
    """
    This method is to get the partner/customer's country name
    """
    order = self.browse(cr, uid, ids)[0]
    country_name = order.partner_id.country_id.name
    print country_name
    return country_name

_columns={
    'country_test':fields.function(get_country, type='char', string='Country'),
}

Error message:

Belgium

2013-11-29 03:49:09,278 5036 ERROR moleac_v1_2 openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\orm.py", line 3604, in read
    result = self._read_flat(cr, user, select, fields, context, load)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\orm.py", line 3724, in _read_flat
    res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\fields.py", line 1137, in get
    elif result.get(id):
AttributeError: 'unicode' object has no attribute 'get'
2013-11-29 03:49:09,282 5036 ERROR moleac_v1_2 openerp.netsvc: 'unicode' object has no attribute 'get'
Traceback (most recent call last):
  File "C:\erpDev\workspace\openerp-7.0\openerp\netsvc.py", line 292, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "C:\erpDev\workspace\openerp-7.0\openerp\service\web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 188, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\orm.py", line 3604, in read
    result = self._read_flat(cr, user, select, fields, context, load)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\orm.py", line 3724, in _read_flat
    res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
  File "C:\erpDev\workspace\openerp-7.0\openerp\osv\fields.py", line 1137, in get
    elif result.get(id):
AttributeError: 'unicode' object has no attribute 'get'

Much appreciate your help!!!!!!!!!!!!!

0
Timo Talvitie
On 11/29/13, 6:36 AM

In your function you need to return a dictionary instead of a string, try something along these lines:

def get_country(self, cr, uid, ids, name, arg, context=None):
    res = {}
    for order in self.browse(cr, uid, ids, context)
        country_name = order.partner_id.country_id.name
        print country_name
        # Now map the country name to the record ID
        # and put this key/value pair to the dictionary
        res[order.id] = country_name
    return res

Hi Timo, thanks very much. Using the dictionary does make it work! Much appreciate your help!!

Yao
on 12/1/13, 9:40 PM

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: 11/29/13, 12:05 AM
Seen: 2028 times
Last updated: 3/16/15, 8:10 AM