2013-02-13 69 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)