Schedule
模型中,我有以下代码
monday = models.BooleanField(default=False)
tuesday = models.BooleanField(default=False)
wednesday = models.BooleanField(default=False)
thursday = models.BooleanField(default=False)
friday = models.BooleanField(default=False)
saturday = models.BooleanField(default=False)
我无法找出检查预约是否已在预定输入日期之间预约的最佳方式。
我不知道问题是否清楚,所以我会留下一个例子:
我希望每周二和周四9:00安排从2017年2月1日到02/24/17的约会。 之前的预约是在2017年1月19日9:00为另一名患者创建的。该应用必须向用户发出此警告。
我想知道是否有更好的方法来检查这个而不是:
appointments = Appointment.objects.filter(date__range=[start_date, end_date])
for appointment in appointments:
if monday and appointment.weekday() == 0:
check if appointment time == input time
do something
elif tuesday and appointment.weekday() == 1:
check if appointment time == input time
do something
elif wednesday and appointment.weekday() == 2:
check if appointment time == input time
do something
elif thursday and appointment.weekday() == 3:
check if appointment time == input time
do something
elif friday and appointment.weekday() == 4:
check if appointment time == input time
do something
elif saturday and appointment.weekday() == 5:
check if appointment time == input time
do something
也许有一种方法可以从数据库中仅检索那些与日程安排输入时间相同的日期之间的约会。
希望这有点可以理解。
答案 0 :(得分:0)
我不确定,我明白了这个问题。但是,也许,您可以将一周中的所有日期与值相关联,获取所有正确的日期,然后按这些值过滤约会:
days = [monday, tuesday, wednesday, thursday, friday, saturday]
days = zip(days, range(6))
true_days = [d[1] for d in days if d[0]]
appointments = appointments.filter(weekday__in=true_days)
所以你只能得到那些约会,其中工作日对应于真正的工作日。
编辑: 我没有注意到工作日是一种方法。您可以在循环中过滤约会:
new_appointments = []
for a in appointments:
if a.weekday() in true_days:
check if a time == input time
do something