在Django Models.py中添加时间

2024-09-30 16:40:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在models.py中尝试了两次减法,但都出错了。这是我一直在工作的模型

class Schedule(BaseModel):
    bus_company_route = models.ForeignKey(BusCompanyRoute, on_delete=models.PROTECT) 
    travel_date_time = models.DateTimeField()

BusCompanyRoute有行程长度

class BusCompanyRoute(BaseModel):
    journey_length = models.TimeField(null=True)

现在,我尝试使用@property decorator以以下方式添加这些时间

@property
def journey_end_time(self):
    return self.travel_date_time.time()+self.bus_company_route.journey_length

但最终得到以下错误警告: Class 'time' does not define '__add__', so the '+' operator cannot be used on its instances 我怎样才能解决它


Tags: selfdatetimeonmodelspropertyroutelength
1条回答
网友
1楼 · 发布于 2024-09-30 16:40:52

^{}更适合存储旅程的持续时间,而不是TimeField,这将为您提供一个时间增量,您可以从日期时间中添加或减去,而TimeField只提供一天中的静态时间

class BusCompanyRoute(BaseModel):
    journey_length = models.DurationField(null=True)

相关问题 更多 >