在 django 應用程式中,我有一個模型函式,用于計算日期時間欄位之間事件的進度。是否可以在達到100后停止進度。例如:
模型.py
start_appointment = models.DateTimeField(default=timezone.now, blank=True)
end_appointment = models.DateTimeField(default=timezone.now, blank=True)
模型函式
def get_progress(self):
if (self.status) == 'New' or (self.status) == 'Finished':
now = timezone.now()
progress = ((timezone.now() - self.start_appointment) / ((self.end_appointment - now) (now - self.start_appointment)))*100
if progress > 100.0:
...
return progress
謝謝
uj5u.com熱心網友回復:
只需使用 Pythonmin()函式即可。如果進度計算較大,這將回傳您的進度計算或 100。
def get_progress(self):
if (self.status) == 'New' or (self.status) == 'Finished':
now = timezone.now()
progress = min(((timezone.now() - self.start_appointment) / ((self.end_appointment - now) (now - self.start_appointment)))*100, 100)
return progress
uj5u.com熱心網友回復:
def get_progress(self):
return ((timezone.now() - self.start_appointment) / ((self.end_appointment - now) (now - self.start_appointment)))*100
def other_process():
while self.get_progress() < 100:
Do stuff here...
return progress
一旦約會結束,這將回傳進度。也許您會想要回傳 True 或其他東西來斷言約會已完成
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/365871.html
