Odoo Help


Sum a treeview field and store it in another field

Francisco Martínez
on 6/11/13, 4:29 PM 3,716 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--
WebVision Enterprise Systems
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Odoo Developer

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

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

0 follower(s)


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