跳至内容
菜单
此问题已终结
2 回复
5569 查看

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.

编写者

@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!

最佳答案

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

形象
丢弃
最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
2
12月 22
15326
1
11月 21
5413
0
1月 21
2659
8
5月 20
8324
0
12月 23
3248