JordanBelf
JordanBelf

Reputation: 3348

How can I loop and access values of fields in OpenERP?

I am learning to develop OpenERP modules, and one of the things I need to do is calculate the average of all the inputs from the user.

My idea was to loop the records while keeping the sum and the count and then make the average, but I can't seem to understand how to access the value for the total field for each record in the sim.students table

Here is part of my code

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

But the result of printing self.browse(cr,uid,id,['total']) returns me browse_record(sim.student, 3)and not the total itself.

I know this must be really simple but I can't seem to figure out how to get to that value.

Any tip much appreciated

Upvotes: 2

Views: 4911

Answers (1)

Stephen
Stephen

Reputation: 2424

So this is what I got from here:

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

where:

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

It returns an object with all the fields accessible by dot notation. So you can do something like:

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

or if you like list comprehensions:

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

Upvotes: 5

Related Questions