Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
3677 Tampilan

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
Buang
Jawaban Terbai

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
Buang
Jawaban Terbai

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
Buang
Penulis

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)

Post Terkait Replies Tampilan Aktivitas
1
Des 16
7891
4
Feb 20
4056
1
Feb 24
1886
2
Apr 24
1983
0
Jul 24
2878