I want users to be able to check the calendar for dates already booked so the person won't be able to book a room if it has been booked already. I used the lte and gte to check for dates but it's very inconsistent.
See what my DB looks like below.
The main problem I'm facing,
From June 2 to June 13 have been booked for hotelroom id 1. It's returning available instead of not available.
>>> start_date='2016-06-02'
>>> end_date='2016-06-13'
>>> check_for_bookings=HotelCalendar.objects.filter(Q(checkin_booked_date__gte=start_date) | Q(checkout_booked_date__lte=end_date), hotelrooms_id=1)
>>> if check_for_bookings:
... print 'not available'
... else:
... print 'available'
...
available
>>>
I selected from June 3 to June 14 and tested it with the below query and it worked. It showed that the room is not available.
>>> start_date='2016-06-03'
>>> end_date='2016-06-14'
>>> check_for_bookings=HotelCalendar.objects.filter(Q(checkin_booked_date__gte=start_date)|Q(checkout_booked_date__lte=end_date), hotelrooms_id=1)
>>> if check_for_bookings:
... print 'not available'
... else:
... print 'available'
...
not available
The question is why did the first query failed to return 'not available' when the dates have been booked.?
What other query can I run to make it efficient?