gpt4 book ai didi

python - 我如何在 OpenERP 中循环访问字段值?

转载 作者:行者123 更新时间:2023-11-28 22:00:27 24 4
gpt4 key购买 nike

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

我知道这一定非常简单,但我似乎无法弄清楚如何获得该值。

非常感谢任何提示

最佳答案

这就是我从 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)

关于python - 我如何在 OpenERP 中循环访问字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14844773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com