openerp错误 AttributeError: 'int'对象没有属性'iteritems

1 投票
1 回答
3465 浏览
提问于 2025-04-18 02:10

我在代码中尝试使用多个参数时遇到了一个错误,我试了很多方法,但还是没法解决这个问题。请告诉我怎么才能修正这个错误。我的代码如下:

class client_client(osv.osv):
    def totalsum(self, cr, uid, ids, field_name, args, context=None):
        tsum = {}
        for dx in self.browse(cr, uid, ids, context=context):
            tsum[dx.id] = (dx.cp1val + dx.cp2val)
            print tsum
        return tsum

    def totalsum_val(self, cr, uid, ids, context=None):
        tqw = {}

        for cl in self.browse(cr, uid, ids, context=None):
            tqw[cl.id] = (cl.cp1val + cl.cp2val + 2)
        return tqw.keys()


    _name = 'client.client'

    _columns = {
        'total': fields.function(totalsum, type='integer', string='Total', help="Total sum",
                                 store={'client.client': (totalsum_val, ['cp1val', 'cp2val'], 10)}, multi='calc'),
        'total2': fields.function(totalsum, type='integer', string='2nd total',
                                  store={'client.client': (totalsum_val, ['cp1val', 'cp2val'], 10)}, multi='calc'),
        'total3': fields.function(totalsum, type='integer', string='3rd total',
                                  store={'client.client': (totalsum_val, ['cp1val', 'cp2val'], 10)}, multi='calc'),
        'cp1val': fields.integer('NO 1', size=45, help="First no to be added", required=True),
        'cp2val': fields.integer('NO 2', size=45, help="Second no to be added", required=True),
    }

    _defaults = {
    }

我现在想做的就是在 totalsum() 函数中把两个数字 cp1val 和 cp2val 相加,然后在 totalsum_val 函数中使用多个参数,把同样的 cp1val 和 cp2val 与整数 2 相加,但我没有成功,只有 totalsum 函数能正确执行,并且我在总字段中得到了它的输出。请帮帮我。

这是日志:

Server Traceback (most recent call last):
File "/home/subash/Openerp_11_12_13/web/addons/web/session.py", line 89, in send
return openerp.netsvc.dispatch_rpc(service_name, method, args)
File "/home/subash/Openerp_11_12_13/server/openerp/netsvc.py", line 292, in dispatch_rpc
result = ExportService.getService(service_name).dispatch(method, params)
File "/home/subash/Openerp_11_12_13/server/openerp/service/web_services.py", line 626, in dispatch
res = fn(db, uid, *params)
File "/home/subash/Openerp_11_12_13/server/openerp/osv/osv.py", line 188, in execute_kw
return self.execute(db, uid, obj, method, *args, **kw or {})
File "/home/subash/Openerp_11_12_13/server/openerp/osv/osv.py", line 131, in wrapper
return f(self, dbname, *args, **kwargs)
File "/home/subash/Openerp_11_12_13/server/openerp/osv/osv.py", line 197, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File "/home/subash/Openerp_11_12_13/server/openerp/osv/osv.py", line 185, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File "/home/subash/Openerp_11_12_13/server/openerp/osv/orm.py", line 4500, in create
self.pool.get(object)._store_set_values(cr, user, ids, fields2, context)
File "/home/subash/Openerp_11_12_13/server/openerp/osv/orm.py", line 4627, in _store_set_values
result = self._columns[val[0]].get(cr, self, ids, val, SUPERUSER_ID, context=context)
File "/home/subash/Openerp_11_12_13/server/openerp/osv/fields.py", line 1144, in get
for field, value in result[id].iteritems():
AttributeError: 'int' object has no attribute 'iteritems'

1 个回答

2

对于多重结果,你需要把那个函数的结果字典改成下面这种形式:

{'id1':{
        'field1':value,
        'field2':value,
        'field3':value,
       },
 'id2':{and so on...
}

所以根据你的例子,你的函数应该像这样:

def totalsum(self,cr,uid,ids,field_name,args,context=None):
     tsum={}
     for dx in self.browse(cr,uid,ids,context=context):
          tsum[dx.id]={
                       'total':#value here,
                       'total2':#value here,
                       'total3':#value here,
                      }
          print tsum
     return tsum

撰写回答