Odoo Help


This community 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.


Sum a treeview field and store it in another field

Francisco Martínez
on 6/11/13, 4:29 PM 2,617 views

I need sum the field "Hours" of all lines on a treeview and store the result in other field ("total_hours") on main table. The main Table have a one2many field linked to second table (treeview).

How can i do this?

Main Table:

class main_table(osv.osv):
    _name = 'sat.main'
    _columns = {
        'name' : fields.char('name', size=256, required=True),
        'total_hours' : fields.float('Producto'),
        'hours_id': fields.one2many('sat.hours', 'hour_id', 'Horas'),

Second Table:

class hours(osv.osv):
    _name = 'sat.hours'
    _columns = {
                'hour_id': fields.many2one('sat.main', 'mainwork'),
                'concept': fields.char('Concept', size=128, required=True),
                'hours': fields.float('hours'),

Grover Menacho

--Grover Menacho--
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 6/11/13, 5:04 PM

First of all on hour_id it's 'sat.main' not 'sat.incidencias'
You can write a default_get function and sum lines there, then you have to return the lines and there you can write on your object.

Francisco Martínez
On 6/12/13, 5:19 AM

I Found a good solution:

def _total_presupuestos(self, cr, uid, ids, field_name, field_value, arg, context=None):
    invoice_obj = self.pool.get('sat.incidencias')
    for records_invoice in invoice_obj.browse(cr, uid, ids):
        for records_invoice_lines in records_invoice.presupuesto_id:
            total += records_invoice_lines.pv_sin_impuestos
            result[records_invoice.id] = total
    return result

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)


Asked: 6/11/13, 4:29 PM
Seen: 2617 times
Last updated: 3/16/15, 8:10 AM