Odoo Help


How to get total of a list?

Arjun Khode
on 5/15/15, 4:10 AM 1,175 views

Hi, I have written a module where there is a list of expenses. It contains 1.name of expense 2.cost

I want to take a variable and add all expenses into it and display it. How to do that?


Here is my py code:

class expenditure_records(osv.Model):



'name':fields.char('Expense name'),

'cost':fields.float('Expenditure cost'),

'expense_id':fields.many2one('expenditure','Expense ID'),



Akhil P Sivan

--Akhil P Sivan--

| 5 4 7
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 5/15/15, 5:13 AM

Hi Arjun,

You want to get the sum of costs in expense records, that related to an expenditure right? Try like the following:

class expenditure_records(osv.Model):
'name':fields.char('Expense name'),
'cost':fields.float('Expenditure cost'),
'expense_id':fields.many2one('expenditure','Expense ID'),
class expenditure(osv.Model):
def _get_total(self, cr, uid, ids,field_name, args, context=None):
expenditure_pool = self.pool.get('expenditure')
res = {}
for exp_id in self.browse(cr, uid, ids, context=context):
s_total = 0
exp = expenditure_pool.browse(cr,uid, exp_id.id, context=None)
for line in exp.expense_ids:
s_total += line.cost
res[exp_id.id] = s_total
return res
'date': fields.date(string="Date"),
'expense_ids':fields.one2many('expenditure.records', 'expense_id', string="Expenses"),
'total_expense':fields.function(_get_total, type="float", string="Total Expenses")




| 5 3 8
Delhi, India

Nothing to say .... :)

On 5/15/15, 10:23 AM

Don't use list for that, but use dictionary , store expense name in key of dictionary and expense value in  value of dictionary

something like

expense_dict = {'exp1' : 120 , 'exp2' : 350.0, 'exp3' : 500}

then for get total you can do like:

total = 0.0
for value in expense_dict.values():
             total = total + value
print "total expense......",total


however if you want sum of list you have inbuilt python method for that, just do like



you need to make sure your list contains only integer or float values.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 5/15/15, 4:10 AM
Seen: 1175 times
Last updated: 5/15/15, 10:30 AM