【发布时间】:2020-06-21 18:02:41
【问题描述】:
我是 python 新手,几天来我无法解决我的简单问题。
我有一个 Event 模型,它应该允许用户预订 timeslot、room 和 event type这将显示在时间表中。
class Event(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()
get_date = models.DateTimeField() # set via form, has year and day
start_time_choices = [('1', '07:30'), ('2', '07:45'), ('3', '08:00'), ('4', '08:15'), ('5', '08:30')]
timeslots = models.TimeField(
choices=start_time_choices,
"%H:%M",
default='1',
null=True)
room_choices = [('1', 'Room 1'), ('2', 'Room 2'), ('3', 'Room 3'), ('4', 'Room 4'), ('5', 'Room 5')]
room = models.CharField(
choices=room_choices,
max_length=1,
default='1',
null=True)
event_choices = [('1', 'Event A'), ('2', 'Event B')] # A is 15min long, B is 30min long
events= models.CharField(
choices=event_choices,
max_length=1,
default='1',
null=True)
现在我想计算这个事件的 start_time 和 end_time 用于日历视图
- 我将以下计算放在哪里?直接在models.py中?
- 如何处理正确的日期计算?
# this is not a real code
@property
def date_calc(self):
start_date = get_date + timeslots # Year & Day + Hour & Min
if events == "Event A":
end_date = start_date + "00:15"
else:
end_date = end_date + "00:30"
return start_time, end_time
【问题讨论】:
标签: python django datetime python-datetime