Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
4 Antwoorden
10542 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer
Beste antwoord

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


best regards,

danielcaceresf@gmail.com

Avatar
Annuleer
Beste antwoord

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
Annuleer
Auteur

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

Gerelateerde posts Antwoorden Weergaven Activiteit
1
jun. 25
14504
3
apr. 25
4443
2
jul. 24
1674
1
jan. 24
1308
1
jul. 22
1809