Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
4 Răspunsuri
11170 Vizualizări

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.

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Cel mai bun răspuns

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


best regards,

danielcaceresf@gmail.com

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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

Related Posts Răspunsuri Vizualizări Activitate
1
iun. 25
15518
3
apr. 25
5732
2
iul. 24
3512
1
ian. 24
1881
1
iul. 22
2319