0

我正在尝试在我的模型中计算和计算 total_hours。我创建了一种计算方法,但是当我这样做时出现错误。错误被称为:unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'我只需要一些帮助来解决这个问题。提前致谢。

这是有问题的代码:

def convert_timedelta_to_hrs(td):
    return decimal(td.total_seconds() / (60 * 60))

class DPRLog(models.Model):
    STATUS_CHOICES = (

        ('PENDING', 'PENDING'),
        ('CANCELLED', 'CANCELLED'),
        ('COMPLETED', 'COMPLETED'),


        )
    TASKS_CHOICES = (
        ('TESTS EXECUTION', 'TESTS EXECUTION'),
        ('TESTS DESIGN', 'TESTS DESIGN'),
        ('MOBILE TESTING WORKSHOP', 'MOBILE TESTING WORKSHOP'),
        ('BENCH ACTIVITY', 'BENCH ACTIVITY'),
        ('DEFECT ANALYSIS','DEFECT ANALYSIS'),
    )

    testuser = models.ForeignKey(User,on_delete = models.CASCADE,unique_for_date= 'reportDate')
    status = models.CharField(max_length=30, choices=STATUS_CHOICES,null=True)
    reportDate = models.DateField(blank=False, null=False)
    login = models.TimeField(blank=False, null=False)
    logout = models.TimeField(blank=False, null=False)
    total_hours = models.DecimalField(max_digits=4,decimal_places=2,null=True,blank=True)
    ot_hours = models.DecimalField(max_digits=4,decimal_places=2,null=True,blank=True)
    mainTasks = models.CharField(max_length=50, blank=False, choices=TASKS_CHOICES, null=True)
    remarks = models.CharField(max_length=30,null=True,blank=True,)

    def __str__(self):
        return f'{self.testuser.full_name} DPR Log'

    def calculate_hours(self):
        total_hours = self.logout - self.login - datetime.timedelta(hours=1)
        total_hours = convert_timedelta_to_hrs(total_hours)
        ot_hours=None

        if total_hours > 8.00:
            ot_hours = self.total_hours - datetime.timedelta(hours=8)
            ot_hours = convert_timedelta_to_hrs(ot_hours)
            total_hours = 8.00

        return total_hours, ot_hours

    def save(self,*args,**kwargs):
        self.total_hours, self.ot_hours = self.calculate_hours()
        super().save(*args, **kwargs)
4

0 回答 0