在openerp fields.function中将字典作为参数

时间:2014-07-11 12:48:14

标签: python odoo openerp-7

'total_resources':fields.function(_total_resources, method=True, string='Total Resources', type='char',)#是我的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 个答案:

答案 0 :(得分:0)

在fields.function方法中,您应该返回一个字典,其中包含一个id列表作为键,以及一个字典,例如:

  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