title: Django query all records and annotate days between given date plus boolean state till date boolean changes
body: <p>I have a model equivalent to this:</p>
  <pre><code>class SomeModel(models.Model):
      reference = models.CharField(max_length=24, db_index=True)
      start_date = models.DateTimeField()
      is_active = models.BooleanField()
  </code></pre>
  <p>For a given query date and <code>is_active</code> status, I would like to return a queryset of the period from the query date back to when <code>is_active</code> changes.</p>
  <p>e.g. If I had a list:</p>
  <pre><code>| reference | start_date | is_active |
  |-----------|------------|-----------|
  | j39djkj   | 2010-12-01 | True      |
  | dkh39d9   | 2010-11-01 | True      |
  | j39djkj   | 2010-09-01 | True      |
  | dkh39d9   | 2010-08-01 | False     |
  | j39djkj   | 2010-05-01 | False     |
  | dkh39d9   | 2010-03-01 | True      |
  | j39djkj   | 2010-02-01 | True      |
  | j39djkj   | 2010-01-01 | True      |
  </code></pre>
  <p>I want to filter by <code>is_active = True</code> when <code>start_date__lte=2010-12-31</code> and get a queryset back like this:</p>
  <pre><code>| reference | start_date | is_active | active_status_days | 
  |-----------|------------|-----------|--------------------|
  | j39djkj   | 2010-09-01 | True      | 122                |
  | dkh39d9   | 2010-11-01 | True      | 61                 |
  </code></pre>
  <p>The number of records is in the tens of millions with about 1 million individual references. Looking for something that is reasonably performant.</p>
  <p>Running Django 2.2 on Postgresql with Django Rest Framework.</p>
  <p>Thanks and appreciated if you can help...</p>