【问题标题】:How do I get total hours from start- end activity ? - Django model我如何从开始-结束活动中获得总小时数? - Django 模型
【发布时间】:2020-11-13 07:59:27
【问题描述】:

我从模型中有以下两个可变天的条目。

class Log_hour(models.Model):
activity_no = models.CharField(max_length=200, null=True)
activityDate = models.DateField(auto_now_add=False, null=True)
start_activity = models.DateField(auto_now_add=False)
end_activity = models.DateField(auto_now_add=False, null=True)
total_hours = ?


def __str__(self):
    return self.activity_no
     

我如何获得总小时数?我的目标是使用 twilio api 将总小时数发送到短信

【问题讨论】:

    标签: python-3.x django-models django-3.1


    【解决方案1】:

    您可以向模型添加一个属性,例如(也适用于日期时间字段):

        @property
        def total_hours(self):
            "Returns the delta between start_activity and end_activity in hours"
            delta = self.end_activity - self.start_activity 
            hours = delta.total_seconds() / 3600
            return hours
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多