[Answered ]-Lte & gte query inconsistent when Crosschecking for dates in Django

2👍

Your conditions have been swapped gte <> lte . The second query worked because there is a matching date '2016-06-14' for hotelrooms_id=1.

But you want to check if start_date and end_date are within range checkin_booked_date to checkout_booked_date:

check_for_bookings = HotelCalendar.objects.filter(checkin_booked_date__lte=start_date, 
                                                  checkout_booked_date__gte=end_date,
                                                  hotelrooms_id=1)

Use exists if you only need to check and not fetch the objects:

if HotelCalendar.objects.filter(checkin_booked_date__lte=start_date,
                                checkout_booked_date__gte=end_date, 
                                hotelrooms_id=1).exists():

Update:

From this SO answer, we can tell if start and end dates overlap with the dates of occupancy of a client:

from datetime import datetime as dt

hotelcalendar = HotelCalendar.objects.filter(hotelrooms_id=1)

start_date = dt.strptime(start_date, '%Y-%m-%d')
end_date = dt.strptime(end_date, '%Y-%m-%d')

if hotelcalendar.checkin_booked_date <= end_date and hotelcalendar.checkout_booked_date >= start_date:
     print "not available"
else:
     print "available"

Update:

I tweaked it this way: I changed ‘filter’ to ‘get’ because it will return ‘AttributeError’. And I used datetime.date() directly. And it worked fine so far!

>>> import datetime
>>> hotelcalendar= HotelCalendar.objects.get(hotelrooms_id=1)
>>> start_date= datetime.date(2016, 06, 14)
>>> end_date= datetime.date(2016, 06, 19)
>>> if hotelcalendar.checkin_booked_date <= end_date and hotelcalendar.checkout_booked_date >= start_date:
...     print 'not available'
... else:
...     print 'available'
...
not available

>>> hotelcalendar= HotelCalendar.objects.get(hotelrooms_id=1)
>>> start_date= datetime.date(2016, 06, 15)
>>> end_date= datetime.date(2016, 06, 19)
>>> if hotelcalendar.checkin_booked_date <= end_date and  hotelcalendar.checkout_booked_date >= start_date:
...     print 'not available'
... else:
...     print 'available'
...
available
>>> hotelcalendar= HotelCalendar.objects.get(hotelrooms_id=3)
>>> start_date= datetime.date(2016, 06, 02)
>>> end_date= datetime.date(2016, 06, 10)
>>> if hotelcalendar.checkin_booked_date <= end_date and hotelcalendar.checkout_booked_date >= start_date:
...     print 'not available'
... else:
...     print 'available'
...
not available
>>>

Leave a comment