title: Django model filtering with F and Q expressions
body: <p>I am making a scheduler in Django and having issues filtering my events for the weekly calendar view. The calendar supports multi-day events, and my current filter doesn't work with this weekly view. </p>
  
  <p>Here is my model:</p>
  
  <pre><code> class Event(models.Model):
      title = models.CharField(max_length=40)
      start = models.DateTimeField()
      end = models.DateTimeField()
      description = models.TextField()
      all_day = models.BooleanField(default=False)
      recuring = models.BooleanField(default=False)
      recuring_end = models.DateTimeField(blank=True, null=True)
  
      def __str__(self):
          return self.title
  
      def get_absolute_url(self):
          return '/cab/event/%i/' % self.id
  </code></pre>
  
  <p>and I'm trying to filter the events that occur during a given week. For single day events I do something like.</p>
  
  <pre><code>events = Event.objects.order_by('start').filter(Q(start__gte=monday) | Q(end__lte=sunday))
  </code></pre>
  
  <p>This works to retrieve all single day events that occur during the week. It also works for multi-day events that either start or stop during the given week. The issue is retrieving objects that start before and finish after the week, but do span the week.</p>
  
  <p>My idea is to try and filter out any event spanning longer than 9 days (ie. would start Sunday of prior week and finish Monday of next week) since I know these are rare and wont completely destroy performance. I want to do this <strong>without specifying a date range</strong> as this is not dynamic.</p>
  
  <p>To try and minimise the performance impact I was trying to use F expressions to evaluate the duration of the event with the start and end of the event. My first idea was to do something like:</p>
  
  <pre><code>my_events = Event.objects.order_by('start').filter(Q(start__gte=monday) | Q(end__lte=sunday) | Q( (F('end_day') - F('start_day')) &gt;= 9 ) )
  </code></pre>
  
  <p>but I get error <code>'bool' object is not iterable</code> </p>
  
  <p>also tried:</p>
  
  <pre><code>my_events = Event.objects.order_by('start').filter(Q(start__gte=monday) | Q(end__lte=sunday) | Q( (F('end_day') - F('start_day')) &gt;= datetime.timedelta(days=9) ) )
  </code></pre>
  
  <p>but get <code>can't compare datetime.timedelta to ExpressionNode</code></p>
  
  <p>Anyone have incite as how to do such a thing?</p>