Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
11041 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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


best regards,

danielcaceresf@gmail.com

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 25
15296
3
thg 4 25
5423
Compute Fields Đã xử lý
2
thg 7 24
2325
1
thg 1 24
1744
1
thg 7 22
2174