Skip to Content
Menu
This question has been flagged
2 Replies
2366 Views

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)

Avatar
Discard
Best Answer

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.
Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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)

Related Posts Replies Views Activity
1
Dec 16
7013
4
Feb 20
2971
1
Feb 24
839
2
Apr 24
573
0
Jul 24
1503