2

我正在学习开发 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),而不是总数本身。

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

非常感谢任何提示

4

1 回答 1

5

所以这就是我从这里得到的:

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)
于 2013-02-13T01:36:05.787 回答