I have two tables with this informations:
Room
id | room name |
---|---|
1 | room 1 |
2 | room 2 |
and a child table with this information:
Reservation
id | room id (reserved_rooms) | from date | to date |
---|---|---|---|
1 | 1 | 2022-05-20 | 2022-05-22 |
2 | 1 | 2022-05-23 | 2022-05-25 |
I want to get available rooms for a certain amount of time:
- Witch doesn't have any reservation
- rooms where no reservation has been made at a specific time such as
2022-05-24
I tried the following code, but the problem is that the booked room is returned with id=1, while it has a reservation on the desired date.
Room.objects.filter(Q(reserved_rooms__isnull=True) |
Q(reserved_rooms__from_date__gt=date) |
Q(reserved_rooms__to_date__lt=date))
This query ignored reservation with id=2, because 2022-05-23 < 2022-05-24 < 2022-05-25
, but reservation with id=1 causes that room with id=1 is returned. because 2022-05-24
not in range 2022-05-20
and 2022-05-22
.
so I expected that the only room with id=2 would be returned, but both 1 and 2 were returned.
What is your suggestion to solve this challenge?