title: How to find avaiable time slots in an Appointments table using Django ORM?
body: <p>I have a Django model that models patients making Appointments with doctors, like so:</p>
  
  <pre><code>class Appointment(Model):
      doctor = models.ForeignKey(User)
      start = models.DateTimeField()
      finish = models.DateTimeField()
      ...
  </code></pre>
  
  <p>I want to find available time slots that have at least a certain length between a range of datetimes, like this: </p>
  
  <pre><code>def find_timeslots(timeslot_length, start_datetime, finish_datetime): 
      ....
  </code></pre>
  
  <p>Some observations:</p>
  
  <ul>
  <li>The difference from start to finish is at most 8 hours</li>
  <li>Appointments can go over the day (e.g., I can have an appointment that starts at 2016-03-21 22:00:00 and ends at 2016-03-22 06:00:00).</li>
  </ul>
  
  <p>My current solution is getting all Appointments from a doctor and iterate from <strong>start_datetime</strong> until <strong>finish_datetime</strong> in <strong>timeslot_length</strong> intervals, checking for conflicts. If it has no conflict, I store a (start, finish) tuple in a list. </p>
  
  <p>It somewhat works, but it doesn't seems like an elegant solution. What's a better approach to this problem?</p>