Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
3338 มุมมอง

Hi forum!


I have this char field:

overtime = fields.Char(readonly=True, default='00:00')

I would need to summarize the values ​​of the overtime field, and I want to print this value in a new field.

This is how the values ​​appear in the overtime field:

02:00
00:00
01:00
00:00
03:00
00:00
00:00
02:00
00:00
01:00
01:00

Is it possible to do it?


Thanks for your help!

Regards,

Steven

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi Steven:

Here's an example of how it can be done. You will need to replace the array with the appropriate recordset returned by a search statement.

from datetime import time

ots = ['02:00','01:50','00:10','03:15']

tot = 0
for ot in ots:
t = time.fromisoformat(ot)
tot += t.hour*60 + t.minute

print('%s:%s' % (str(int(tot/60)).zfill(2), str(tot%60).zfill(2)))

Hope this helps.

อวตาร
ละทิ้ง
ผู้เขียน

Thank you Paresh Wagh! Thanks for the great help!

ผู้เขียน

I had to convert the string type to a float. But when I reference the sum_50_overtime field with a related field, it doesn't get the value.

0 is the value of the sum_50_overtime field, but i don't know why.

But when I print to the console, it gets the correct value, only the related field does not.

sum_50_overtime = fields.Float(compute='_compute_sum_50')

@api.multi
@api.depends('overtime_50', 'sum_50_overtime')
def _compute_sum_50(self):
ots = []
for record in self:
ots.append(record.overtime_50)
tot = 0
for ot in ots:
t = parser.parse(ot)
tot += t.hour * 60 + t.minute
s = ('%s:%s' % (str(int(tot / 60)).zfill(2), str(tot % 60).zfill(2)))
time = float(s.replace(":", ""))
record.sum_50_overtime = time
print(record.sum_50_overtime)

The related field :

overtime_weekdays = fields.Float(related='attendance_ids.sum_50_overtime')

Thank you for your help!

Related Posts ตอบกลับ มุมมอง กิจกรรม
1
มี.ค. 18
6958
3
ก.ค. 24
22622
3
ก.พ. 25
1010
0
ก.พ. 25
1255
0
ม.ค. 25
1096