Skip to Content
Menu
This question has been flagged
4 Replies
9470 Views

Am aware SUM attribute can be set only on physical fields, which in turn will be saved into the database, so when you group-by is applied in the list-view, Odoo will sum up the float/integer fields.

But how do I achieve the same on Virtual/Computed fields.

Note: I don't want to save the field, in order to apply SUM attribute, but looking for other alternate way.

Avatar
Discard
Best Answer

Hi Deep,

If you want to create a field without saving into the database, you can use the computed field.

This is the reference https://www.odoo.com/documentation/8.0/reference/orm.html#creating-models

sum = fields.Float(compute='_compute_sum')

@api.depends('value1', 'value2')
def _compute_sum(self):
    for record in self:
        record.sum = record.value1 + record.value2

* computed fields are not stored by default, they are computed and returned when requested

Avatar
Discard
Best Answer

Hi Deep, I have the same problem. You found the solution???. Please, help me.


best regards,

danielcaceresf@gmail.com

Avatar
Discard
Best Answer

Add one more argument in your field declaration

sum = fields.Float(store=True)

If above solution not work then try this,

override create() method. Set values to SUM fields in create() like you sets in compute method.

@api.depends('f1', 'f2')

def _sum_compute(self):

        for r in self:

            if r.f1 and r.f2:

                r.sum = r.f1 + r.f2


@api.model

def create(self, vals):

    if vals['f1'] and vals['f2']:

        vals['sum'] = vals['f1'] + vals['f2']

             res = super(your_python_class, self).create(vals)

             return res

Avatar
Discard
Author

Thanks for your reply, but I clearly mentioned, I want sum attribute for non-stored fields.

Related Posts Replies Views Activity
2
Jul 24
414
1
Jan 24
423
2
Apr 24
1951
1
Jul 22
895
0
Oct 24
12818