Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Computed field that sums records

By
José
on 1/8/15, 3:21 PM 616 views

Hello, I have Odoo8.

In a model A I have a One2many field and a Many2many field. The related models has fields which are float values. I want to add a computed field in the model A that sums all the float values of the other two models related. How can I do?

I do this but not works (in cases return value 0 and in cases only one value, not the sum)

    @api.one
    @api.depends('pays')
    def _actualiza_monto(self):
        total_comprado = 0
        for compra in self.pays
            total_comprado = total_comprado + compra.amount_total

    self.computed_field = total_comprado

@vasanth, If I use @api.one means that I use one self record, isn't it? I want to use one record of self (sale.order) which has a collection of pays.

José
on 1/9/15, 7:24 AM

@zbik, I have the tab formatting correctly, when I do copy paste to the forum I do the mistake... in my .py file I have it correctly. Sorry!

José
on 1/9/15, 8:12 AM
0

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 1/9/15, 7:45 AM

Hi Jose, probably your problem is related to the tab formatting, last line is without tab and is outside of method?

@api.one
@api.depends('pays')
def _actualiza_monto(self):
    total_comprado = 0
    for compra in self.pays
        total_comprado = total_comprado + compra.amount_total
    self.computed_field = total_comprado

0

Vasanth

--Vasanth--
3458
| 5 3 8
Pondicherry, India
--Vasanth--
Vasanth
On 1/9/15, 6:56 AM

Hi Jose ,

If you are using for loop means you should use @api.multi..If you use @api.one means it returns only one value

So the cosde should be:

    @api.multi
    @api.depends('pays')
    def _actualiza_monto(self):
        total_comprado = 0
        for compra in self.pays
            total_comprado = total_comprado + compra.amount_total

     self.computed_field = total_comprado

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/8/15, 3:21 PM
Seen: 616 times
Last updated: 3/16/15, 8:10 AM