簡體   English   中英

在 django Models.py 中添加時間

[英]Add time in django Models.py

我嘗試在 models.py 中減去兩次,但出現錯誤。 這是我一直在工作的 model。

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裝飾器添加這些時間

@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我該如何解決?

DurationField更適合存儲旅程的持續時間而不是TimeField ,這將為您提供一個 timedelta ,您可以在日期時間中添加或減去它,而TimeField僅提供 static 一天中的時間

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM