TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

后端 未结 1 1090
忘了有多久
忘了有多久 2021-02-19 12:12

my models.py:

class Attendancename(models.Model):
    teacher_name = models.ForeignKey(Teachername, default=\'Ram\')
    date = models.DateField(\'Date\', defaul         


        
相关标签:
1条回答
  • 2021-02-19 12:31

    It's because you cannot subtract a datetime.time from a datetime.time. Convert them to datetime.datetime objects and it'll return a datetime.timedelta object that you could use.

    If you're lucky enough to be using Django 1.8, they now have a DurationField that can be used.

    Failing that, I would recommend converting the timedelta into either seconds or a floating point representation so you can actually store it to the database.

    EDIT: Pulled up in comments for half arsing an answer.

    For example - if you want to store the number of (integer) seconds, you can convert from a TimeDelta by using secs = td // timedelta(seconds=1).

    0 讨论(0)
提交回复
热议问题