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