title: Django model querying for first N rows that sum up to a given number
body: <p>I am sing Django 3.2</p>
  <p>I have a model like this:</p>
  <pre><code>class MyModel(models.Model):
      name = models.CharField(max_length=255)
      num_visits  = models.PositiveSmallNumber()
      created_at  = models.DateTimeField()
  
      class Meta:
          ordering = ['created_at']
  </code></pre>
  <p>I want to select the first N rows where the <strong>CUMULATIVE SUM</strong> 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.</p>
  <p>How can I write a query that fetches the first N rows where the SUM of visits is a specified number?</p>
  <pre><code>def get_rows_satisfying_visit_count(cumulative_sum_in_field=100):
      MyModel.objects.filter(/* what ? */)
  </code></pre>