В openerp fields.функция принимает словаря как arugument

'total_resources': fields.function(_total_resources, method=True, string='Total Resources', type='char',) # is my fields.function

def _total_resources(self, cr, uid, ids, field_name, arg, context):
 res = {}
 reqrd_bffr = 0
 trng_bffr = 0
 billable = 0
 list_resources = self.browse(cr,uid,ids[0]).resource_all_id
 print"list_resources",list_resources
 for status_chk in list_resources :
 status_resrc = status_chk.status_id.name
 if status_resrc == 'Required Buffer':
 reqrd_bffr=reqrd_bffr + 1
 elif status_resrc == 'Training Buffer':
 trng_bffr=trng_bffr + 1
 elif status_resrc == 'Billable':
 billable=billable + 1
 total = len(list_resources)
res = {'total_resources' : total,'req_buffr':reqrd_bffr,'trng_buffr': trng_bffr, 'billable' : billable}
 return res

когда я выполняю это, он дает ошибку, как показано ниже

Ошибка сервера OpenERP

Client Traceback (most recent call last):
File "/opt/openerp/openerp_7/openerp/addons/web/http.py", line 204, in dispatch
 response["result"] = method(self, **self.params) 
 File "/opt/openerp/openerp_7/openerp/addons/web/controllers/main.py", line 1128, in call_kw
 return self._call_kw(req, model, method, args, kwargs) 
 File "/opt/openerp/openerp_7/openerp/addons/web/controllers/main.py", line 1114, in _call_kw
 records = req.session.model(model).read(*args, **kwargs) 
 File "/opt/openerp/openerp_7/openerp/addons/web/session.py", line 42, in proxy
 result = self.proxy.execute_kw(self.session._db, self.session._uid, self.session._password, self.model, method, args, kw) 
 File "/opt/openerp/openerp_7/openerp/addons/web/session.py", line 30, in proxy_method
 result = self.session.send(self.service_name, method, *args) 
 File "/opt/openerp/openerp_7/openerp/addons/web/session.py", line 103, in send
 raise xmlrpclib.Fault(openerp.tools.ustr(e), formatted_info)
Server Traceback (most recent call last):
 File "/opt/openerp/openerp_7/openerp/addons/web/session.py", line 89, in send
 return openerp.netsvc.dispatch_rpc(service_name, method, args)

 File "/opt/openerp/openerp_7/openerp/netsvc.py", line 296, in dispatch_rpc
 result = ExportService.getService(service_name).dispatch(method, params)

 File "/opt/openerp/openerp_7/openerp/service/web_services.py", line 626, in dispatch
 res = fn(db, uid, *params)

 File "/opt/openerp/openerp_7/openerp/osv/osv.py", line 190, in execute_kw
 return self.execute(db, uid, obj, method, *args, **kw or {})

 File "/opt/openerp/openerp_7/openerp/osv/osv.py", line 132, in wrapper
 return f(self, dbname, *args, **kwargs)

 File "/opt/openerp/openerp_7/openerp/osv/osv.py", line 199, in execute
 res = self.execute_cr(cr, uid, obj, method, *args, **kw)

 File "/opt/openerp/openerp_7/openerp/addons/audittrail/audittrail.py", line 532, in execute_cr
 return fct_src(cr, uid, model, method, *args, **kw)

 File "/opt/openerp/openerp_7/openerp/osv/osv.py", line 187, in execute_cr
 return getattr(object, method)(cr, uid, *args, **kw)

 File "/opt/openerp/openerp_7/openerp/osv/orm.py", line 3679, in read
 result = self._read_flat(cr, user, select, fields, context, load)

 File "/opt/openerp/openerp_7/openerp/osv/orm.py", line 3803, in _read_flat
 record[f] = res2[record['id']]
KeyError: 61
1 ответ

В методе fields.function вы должны вернуть словарь со списком идентификаторов как ключа и словаря для таких значений, как:

def _total_resources(self, cr, uid, ids, field_name, arg, context):
 res = {}
 reqrd_bffr = 0
 trng_bffr = 0
 billable = 0
 for id in ids:
 res[id] = {
 'total_resources' : 0,
 'req_buffr':0,
 'trng_buffr': 0,
 'billable' : 0
 }
 list_resources = self.browse(cr,uid,ids[0]).resource_all_id
 print"list_resources",list_resources
 for status_chk in list_resources :
 status_resrc = status_chk.status_id.name
 if status_resrc == 'Required Buffer':
 reqrd_bffr=reqrd_bffr + 1
 elif status_resrc == 'Training Buffer':
 trng_bffr=trng_bffr + 1
 elif status_resrc == 'Billable':
 billable=billable + 1
 total = len(list_resources)
 res[id] = {'total_resources' : total,'req_buffr':reqrd_bffr,'trng_buffr': trng_bffr, 'billable' : billable}
 return res

licensed under cc by-sa 3.0 with attribution.