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

Odoo v15

I create a "Margin 2 [x_studio_margen_2]" field at "sale.order" that is supposed to be the sum of every field "Total Margin 2 [x_studio_total_margen_2" at "sale.order.line".

When I add one product, everything is ok, but the problem comes when I add a second product.

I though that it would work but it release an "Expected singleton" error

total = 0
for record in self:
    total += record.order_line.x_studio_total_margen_2
    record['x_studio_margen_2'] = tot

I've read another post with same error but not the same case and I haven't fix it.

形象
丢弃
最佳答案

When you access the "order_line" One2many field, it gives you the number of recordsets (in your case 2 recordsets because you have added 2nd product also).

So you need to use a loop on "order_line" to access every line.

Try the following code:

for record in self:
total = 0
for line in record.order_line:
total += line.x_studio_total_margen_2
record['x_studio_margen_2'] = total

I am sure this will work as you expect.

形象
丢弃
编写者

Thanks a lot!!

That's true, it is now working but another problem happens.

"x_studio_margen_2" is not being updating as "margin" default field does.

For example:

A product with a cost by default 2€ but without a default price_unit that appears at the order line with a "x_studio_total_margen_2" = -2€, so "x_studio_margen_2" now is -2€.

So if I add the price_unit of it product as 3€ it's supposed to have a "x_studio_margen_2" of 1€ but the operation that it does is "-2€ + 1€= so the result is "x_studio_margen_2" = -1€.

Another example:

If I delete the product, "x_studio_margen_2" is still with the -2€, so if I add a product with 3€ of margin ("x_studio_total_margen_2)", now the "x_studio_margen_2" will be -2 + 3 = 1€.

I've tried to only use the formula of "x_studio_total_margen_2" while number is more than 0, but it doesn't work when I delete any product.

Thanks again!

I would suggest to accept this and create another post.

相关帖文 回复 查看 活动
2
1月 24
5377
0
6月 23
2000
0
4月 22
2530
1
4月 19
6168
2
7月 25
3770