Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
2255 Näkymät

I added a monetary field (x_studio_total_price) to the stock moves of a delivery order, to show the prices of all delivered products. Now, I want this sum to be saved and shown as a variable (total_sum) in the delivery order itself, to see the total price of the delivery order.

First, I tried using a related field, but since there is only a reference from the stock picking model to the stock moves model (and not the other way around), this was no option.

Next, I tried using a compute field:

​total_sum = 0
​for rec in self.stock_move:
​total_sum += rec.x_studio_total_price
self.x_studio_total_sum = total_sum

But keep having this value error:

ValueError: forbidden opcode(s) in 'total_sum = 0\nfor rec in self.stock_move:\n  total_sum += rec.x_studio_total_price\nself.x_studio_total_sum = total_sum': STORE_ATTR


What am I doing wrong? Is there another way to easily save an aggregated sum in a variable?

Avatar
Hylkää
Tekijä Paras vastaus

I found a solution!

total_sum = 0
for move in self.move_ids:
​total_sum += move.x_studio_total_price
for rec in self:
​rec['x_studio_total_sum'] = total_sum

By changing self.x_studio_total_sum into rec['x_studio_total_sum'], the opcode error was solved. Afterwards I found out that I could iterate over the move_ids of the stock_picking object to access the different prices.

Avatar
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
elok. 21
6347
0
helmik. 17
5720
2
helmik. 24
3385
1
kesäk. 22
2974
0
marrask. 17
3362