跳至內容
選單
此問題已被標幟
2 回覆
3659 瀏覽次數

Hello everyone,

i created stored computed field that get total value based on lines and some conditions the compute function work well, but when remove the line the computed field value not deleted.

any idea ? 

function code:

@api.depends('res_id', 'res_id.consumption_rate', 'res_id.resource_type')
def get_total_values(self):
hour_values = []
ton_values = []
for line in self.res_id:
if line.consumption == 'hour':
hour_values.append(line.consumption_rate)
print(hour_values)
self.total_per_hour_cost = sum(hour_values)
else:
if line.consumption == 'ton':
ton_values.append(line.consumption_rate)
print(ton_values)
self.total_per_ton_cost = sum(ton_values)

頭像
捨棄
最佳答案

Hi Loay eIhaj,

Initially, Please try to set default value for the functional field in the function method.
Like :

@api.depends('res_id', 'res_id.consumption_rate', 'res_id.resource_type')
def get_total_values(self):
hour_values = []
ton_values = []
self.total_per_hour_cost = 0.0
self.total_per_ton_cost = 0.0
for line in self.res_id:
if line.consumption == 'hour':
hour_values.append(line.consumption_rate)
print(hour_values)
self.total_per_hour_cost = sum(hour_values)
else:
if line.consumption == 'ton':
ton_values.append(line.consumption_rate)
print(ton_values)
self.total_per_ton_cost = sum(ton_values)



Hope it will help you. Please Up vote.
頭像
捨棄
最佳答案

If you are creating a stored computed field, make sure you have given depends so that when dependent field is changed, value will be recomputed. Also provide your code snippet as well so it's easier to understand the problem rather than guess what might have gone wrong.

頭像
捨棄
作者

function code:

@api.depends('res_id', 'res_id.consumption_rate', 'res_id.resource_type')
def get_total_values(self):
hour_values = []
ton_values = []
for line in self.res_id:
if line.consumption == 'hour':
hour_values.append(line.consumption_rate)
print(hour_values)
self.total_per_hour_cost = sum(hour_values)
else:
if line.consumption == 'ton':
ton_values.append(line.consumption_rate)
print(ton_values)
self.total_per_ton_cost = sum(ton_values)

相關帖文 回覆 瀏覽次數 活動
1
12月 16
7879
4
2月 20
4044
1
2月 24
1876
2
4月 24
1961
0
7月 24
2849