I am sing Django 3.2
I have a model like this:
class MyModel(models.Model):
name = models.CharField(max_length=255)
num_visits = models.PositiveSmallNumber()
created_at = models.DateTimeField()
class Meta:
ordering = ['created_at']
I want to select the first N rows where the CUMULATIVE SUM of the number of visits = 100 (as an example) A trivial (but probably DB intensive way) to do it would be to iterate through the rows in the table (i.e. elements of the queryset) - but I don't want to do that - for reasons that should be obvious.
How can I write a query that fetches the first N rows where the SUM of visits is a specified number?
def get_rows_satisfying_visit_count(cumulative_sum_in_field=100):
MyModel.objects.filter(/* what ? */)