Django lte & gte failing When Checking For Dates Range
<p>I want to check the calendar for dates already booked so a user won't be able to book a room if it has been booked already. I used the lte and gte to check for dates and I'm not happy with the result so far.</p>

<p>Models:</p>

<pre><code>class HotelCalendar(models.Model):
    user=models.ForeignKey(User)
    hotelrooms=models.ForeignKey(HotelRooms)
    checkin_booked_date=models.DateField(max_length=20, help_text='yy-mm-dd e.g. 2014-02-01')
    checkout_booked_date=models.DateField(max_length=20, help_text='yy-mm-dd e.g. 2014-02-01')
</code></pre>

<p>For the hotelroom with id 1, dates in the calendar are, </p>

<pre><code>checkedin_booked_date= 2016-06-01   
checkedout_booked_date 2016-06-14
</code></pre>

<p>You can also check the attached file to see how my DB looks like.
<a href="https://i.stack.imgur.com/iCETG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iCETG.png" alt="enter image description here"></a></p>

<p><strong>The Problem</strong></p>

<p>In my db shell, I ran the below code and I'm getting 'dates available' instead of 'booked already', because the date is still within the range. </p>

<pre><code>&gt;&gt;&gt; import datetime
&gt;&gt;&gt; start_date= datetime.date(2016, 6, 1)
&gt;&gt;&gt; end_date= datetime.date(2016, 6, 5)
&gt;&gt;&gt; check_for_bookings=HotelCalendar.objects.filter(Q(checkin_booked_date__gte=start_date), Q(checkout_booked_date__lte=end_date), hotelrooms_id=1)
&gt;&gt;&gt; if check_for_bookings:
...     print 'booked already'
... else:
...     print 'available'
...
   available
&gt;&gt;&gt;
</code></pre>

<p>Any idea of what am missing?</p>

<p><strong>UPDATE:</strong></p>

<p>I tried the solution below but it failed to confirm that the dates within June 2 and June 13 have been booked for hotelroom id 1. It's returning available instead of not available.</p>

<pre><code>&gt;&gt;&gt; start_date='2016-06-02'
&gt;&gt;&gt; end_date='2016-06-13'
&gt;&gt;&gt; check_for_bookings=HotelCalendar.objects.filter(Q(checkin_booked_date__gte=start_date) | Q(checkout_booked_date__lte=end_date), hotelrooms_id=1)
&gt;&gt;&gt; if check_for_bookings:
...     print 'not available'
... else:
 ...     print 'available'
 ...
 available
 &gt;&gt;&gt;
</code></pre>

<p>Another thing is after I changed the end and start date and run the query, it worked. It showed that the room is not available.</p>

<pre><code>&gt;&gt;&gt; start_date='2016-06-03'
&gt;&gt;&gt; end_date='2016-06-14'
&gt;&gt;&gt; check_for_bookings=HotelCalendar.objects.filter(Q(checkin_booked_date__gte=start_date)|Q(checkout_booked_date__lte=end_date), hotelrooms_id=1)
&gt;&gt;&gt; if check_for_bookings:
...     print 'not available'
... else:
...     print 'available'
...
not available
</code></pre>

<p>Could this be a bug?</p>