2013-02-13 65 views
2

我正在学习开发OpenERP模块,我需要做的事情之一是计算用户输入的所有平均值。如何循环访问OpenERP中的字段值?

我的想法是循环记录,同时保持总和和计数,然后进行平均,但我似乎无法理解如何访问值的total场的每个记录在sim.students

这里是我的代码

def get_text(self, cr, uid, ids, fields, arg, context): 
    result = {} 
    i = 0 
    for id in ids: 
     print self.browse(cr,uid,id,['total']) 
     print id 
     i = i+1 
     print i 

    return result 

但印刷self.browse(cr,uid,id,['total'])返回我browse_record(sim.student, 3),而不是总本身的结果的一部分。

我知道这一定很简单,但我似乎无法弄清楚如何达到这个价值。

任何提示赞赏

+0

什么样的对象是自我? – Raufio 2013-02-13 01:09:01

+0

是班学生(osv.osv)的一部分:'这有帮助吗? – JordanBelf 2013-02-13 01:12:31

+0

是的。 self.browse(cr,uid,id).total做什么,如果有的话? – Raufio 2013-02-13 01:24:43

回答

5

所以这是我从here有:

browse(cr ,uid, select, context=None, list_process=None, fields_process=None) 

其中:

cr = database cursor 
uid = user id 
select = id or list of ids 
context = context arguments like lang, time zone 

返回将全部由点号访问的字段的对象。所以,你可以这样做:

records = self.browse(cr, uid, ids) 
for rec in records: 
    print rec.total 
    print rec.otherfield 

,或者如果你喜欢列表解析:

records = self.browse(cr, uid, ids) 
totals = [rec.total for rec in records] 
average = sum(totals)/len(totals)