Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
4 Відповіді
10715 Переглядів

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.

Аватар
Відмінити
Найкраща відповідь

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

Аватар
Відмінити
Найкраща відповідь

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


best regards,

danielcaceresf@gmail.com

Аватар
Відмінити
Найкраща відповідь

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

Аватар
Відмінити
Автор

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

Related Posts Відповіді Переглядів Дія
1
черв. 25
14906
3
квіт. 25
4872
Compute Fields Вирішено
2
лип. 24
1901
1
січ. 24
1510
1
лип. 22
1972