我想顯示未在模板中預訂約會的日期。出于這個原因,我到目前為止:
# Collect only the dates so that i can find what is not in that range.
example_dates = Appointment.objects.values_list('start_appointment', flat=True)
# Initialize start and end date
start_date = datetime.date.today()
end_date = start_date datetime.timedelta(days=5)
# Initialize new list that will include records does not exists
not_found_dates = []
# Loop through in date range and if date does not exists
# Create a dict and add it to the list
for n in range(int((end_date - start_date).days)):
new_date = start_date datetime.timedelta(days=n)
if new_date not in not_found_dates:
not_found_dates.append(new_date)
print(new_date)
# Get original queryset
examples = Appointment.objects.filter(start_appointment__range=(start_date, end_date)).values('start_appointment')
print(examples)
# Convert it to a list
examples = list(examples)
return render(request, 'appointments/add-appointment.html', {'examples': examples, 'not_found_dates': not_found_dates})
當我new_date從回圈中列印時,我得到:2021-11-22 2021-11-23 2021-11-24 2021-11-25 2021-11-26
來自examples回傳的查詢表明我在 2021-11-23 的范圍 (2) 和 2021-11-22 的 (1) db 中有 3 個約會。
是否可以顯示未預約的日期,即2021-11-24, 2021-11-25, 2021-11-26。
uj5u.com熱心網友回復:
您將獲得一個定義的時間范圍內的日期串列(持續時間為 num_days),其中的日期不屬于查詢的一部分:
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(Transaction.objects.values_list("start_appointment", flat=True).distinct())
set(timeframe) - set(exclude)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/363348.html
標籤:姜戈 列表 循环 约会时间 django-queryset
