This question has been flagged
1 Reply
6373 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!!!!!!!!!!!!!

Avatar
Discard
Best Answer

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
Avatar
Discard
Author

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