私は、ユーザーが埋められていないタイムスロットにサインアップできる簡単な予約アプリを持っています。ユーザーは日付を選択し、その日に使用可能なタイムスロットをデータベースに照会します。まだ利用可能なタイムスロットを取得するためにクエリを作成するにはどうすればよいですか?
models.py
class Appointment(models.Model):
TIMESLOT_LIST = (
(1, '10:00 – 11:00'),
(2, '11:00 – 12:00'),
(3, '12:00 – 13:00'),
(4, '13:00 – 14:00'),
(5, '14:00 – 15:00'),
(6, '15:00 – 16:00'),
(7, '16:00 – 17:00'),
(8, '17:00 – 18:00'),
(8, '18:00 – 19:00'),
)
date = models.DateField(default=date.today)
timeslot = models.IntegerField(choices=TIMESLOT_LIST, null=True)
views.py
def make_appointment(request):
all_appointments = Appointment.objects.values_list('timeslot')
appointments = Appointment.objects.filter(user=request.user)
data_input = request.GET.get('date')
available_appointments = Appointment.objects.filter(
date = data_input
).exclude(timeslot = appointments).values_list(
'timeslot'
).order_by('timeslot')
return TemplateResponse(
request,
'scheduling/appointment.html',
{
"appointments" : appointments,
"all_appointments" : all_appointments,
"data_input": data_input
}
)
次のようにして、利用可能な時間の新しいリストを作成できます。
available_appointments = [
(value, time) for value, time in TIMESLOT_LIST if value not in all_appointments
]
または、タプルが必要な場合。
tuple(
(value, time) for value, time in TIMESLOT_LIST if value not in all_appointments
)
次に、テンプレートでそれらの選択肢を提供して、人々が選択できるようにします。
ターミナルからの例。
>>> [(value, time) for value, time in TIMESLOT_LIST if value not in [1, 2, 3]]
[(4, '13:00 – 14:00'), (5, '14:00 – 15:00'), (6, '15:00 – 16:00'), (7, '16:00 – 17:00'), (8, '17:00 – 18:00'), (8, '18:00 – 19:00')]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加