Django - 显示约会的日期和时间
Django - Show date and time of appointment
我目前正在显示所有未预约的日期。
from datetime import date, timedelta
num_days = 5
start_date = date.today()
timeframe = [start_date + timedelta(days=d) for d in range(num_days)]
exclude = list(Appointment.objects.values_list("start_appointment__date", flat=True).distinct())
set(timeframe) - set(exclude)
任命模式
class Appointment(models.Model):
user = models.ForeignKey(Account, on_delete=models.CASCADE)
seat = models.ForeignKey(Seat, on_delete=models.SET_NULL, null=True)
work = models.ManyToManyField(Therapy, related_name='work', verbose_name=_('Work'), blank=True)
start_appointment = models.DateTimeField(default=timezone.now, blank=True)
end_appointment = models.DateTimeField(default=timezone.now, blank=True)
例如我已经预约了:
[datetime.date(2021, 12, 8), datetime.date(2021, 12, 7), datetime.date(2021, 12, 7), datetime.date(2021, 12, 7)]
未预定日期:
{datetime.date(2021, 12, 10), datetime.date(2021, 12, 9), datetime.date(2021, 12, 11)}
我还想显示未预订日期的可用时间,时间间隔介于 11:00AM 到 21:00PM:.
当前的实现是否可行?如果是,我该如何实现?如果不能,您能否建议另一种方法?
谢谢
如果您需要显示可用时间,我建议您按日期分隔您的回复,因此如果您有一天像 datetime(2021, 12, 8) 并且您想显示上午 11 点到晚上 21 点之间的时间,您可以使用类似于:
from datetime import date, datetime, time
date_of_reference = datetime(2021, 12, 8, 0, 0) #your the object of database
initial_time = datetime.combine(date_of_reference.date(), time(11, 0))
final_time = datetime.combine(date_of_reference.date(), time(21,0))
reserved_times = [datetime(2021, 12, 8, 11, 0), datetime(2021, 12, 8, 15, 0)] #reserved_times from database here
hours_between = [datetime.combine(date_of_reference.date(), time(hour + initial_time.hour, 0)) for hour in range(((final_time - initial_time).seconds // 3600) + 1)]
avaliable_hours = [hour for hour in hours_between if hour not in reserved_times]
在你的代码中会是这样的
from datetime import datetime, timedelta
from .models import Appointment
# views
def check_available_hours(request):
day_request = request.GET.get('day','') #day in format yyyy-MM-dd
date_of_reference = datetime.strptime(day_request, "%Y-%m-%d")
initial_date_time = date_of_reference
final_date_time = date_of_reference + timedelta(hours=23)
appointments_of_day = Appointment.objects.filter(
start_appointment__gte=initial_date_time,
start_appointment__lte=final_date_time
)
reserved_times = [
appointment.start_appointment + timedelta(minutes=10*i) for appointment in appointments_of_day for i in range((appointment.end_appointment - appointment.start_appointment).seconds//600)]
# same as above but more undestandeble
# for appointment in appointments_of_day:
# for i in range((appointment.end_appointment - appointment.start_appointment)//600):
# reserved_times.append(appointment.start_appointment + timedelta(minutes=10*i))
hours_between = [initial_date_time + timedelta(minutes=10*i) for i in range(((final_date_time - initial_date_time).seconds // 600) + 1)]
avaliable_hours = [hour.time() for hour in hours_between if hour not in reserved_times]
return render(request, "name_template.html", {"hours": avaliable_hours})
如果您的约会持续时间不是 10 的倍数,您必须更改代码以控制每分钟而不是上面示例中的 10 分钟
我目前正在显示所有未预约的日期。
from datetime import date, timedelta
num_days = 5
start_date = date.today()
timeframe = [start_date + timedelta(days=d) for d in range(num_days)]
exclude = list(Appointment.objects.values_list("start_appointment__date", flat=True).distinct())
set(timeframe) - set(exclude)
任命模式
class Appointment(models.Model):
user = models.ForeignKey(Account, on_delete=models.CASCADE)
seat = models.ForeignKey(Seat, on_delete=models.SET_NULL, null=True)
work = models.ManyToManyField(Therapy, related_name='work', verbose_name=_('Work'), blank=True)
start_appointment = models.DateTimeField(default=timezone.now, blank=True)
end_appointment = models.DateTimeField(default=timezone.now, blank=True)
例如我已经预约了:
[datetime.date(2021, 12, 8), datetime.date(2021, 12, 7), datetime.date(2021, 12, 7), datetime.date(2021, 12, 7)]
未预定日期:
{datetime.date(2021, 12, 10), datetime.date(2021, 12, 9), datetime.date(2021, 12, 11)}
我还想显示未预订日期的可用时间,时间间隔介于 11:00AM 到 21:00PM:.
当前的实现是否可行?如果是,我该如何实现?如果不能,您能否建议另一种方法?
谢谢
如果您需要显示可用时间,我建议您按日期分隔您的回复,因此如果您有一天像 datetime(2021, 12, 8) 并且您想显示上午 11 点到晚上 21 点之间的时间,您可以使用类似于:
from datetime import date, datetime, time
date_of_reference = datetime(2021, 12, 8, 0, 0) #your the object of database
initial_time = datetime.combine(date_of_reference.date(), time(11, 0))
final_time = datetime.combine(date_of_reference.date(), time(21,0))
reserved_times = [datetime(2021, 12, 8, 11, 0), datetime(2021, 12, 8, 15, 0)] #reserved_times from database here
hours_between = [datetime.combine(date_of_reference.date(), time(hour + initial_time.hour, 0)) for hour in range(((final_time - initial_time).seconds // 3600) + 1)]
avaliable_hours = [hour for hour in hours_between if hour not in reserved_times]
在你的代码中会是这样的
from datetime import datetime, timedelta
from .models import Appointment
# views
def check_available_hours(request):
day_request = request.GET.get('day','') #day in format yyyy-MM-dd
date_of_reference = datetime.strptime(day_request, "%Y-%m-%d")
initial_date_time = date_of_reference
final_date_time = date_of_reference + timedelta(hours=23)
appointments_of_day = Appointment.objects.filter(
start_appointment__gte=initial_date_time,
start_appointment__lte=final_date_time
)
reserved_times = [
appointment.start_appointment + timedelta(minutes=10*i) for appointment in appointments_of_day for i in range((appointment.end_appointment - appointment.start_appointment).seconds//600)]
# same as above but more undestandeble
# for appointment in appointments_of_day:
# for i in range((appointment.end_appointment - appointment.start_appointment)//600):
# reserved_times.append(appointment.start_appointment + timedelta(minutes=10*i))
hours_between = [initial_date_time + timedelta(minutes=10*i) for i in range(((final_date_time - initial_date_time).seconds // 600) + 1)]
avaliable_hours = [hour.time() for hour in hours_between if hour not in reserved_times]
return render(request, "name_template.html", {"hours": avaliable_hours})
如果您的约会持续时间不是 10 的倍数,您必须更改代码以控制每分钟而不是上面示例中的 10 分钟