[Answered ]-Scheduling Filter on Database with Time Interval

2👍

SQL doesn’t have a way to ask for every tenth, so ORMs won’t typically give you a way to express that need. You could do that work in Python:

samples = list(Weather.objects.filter().exclude(time__lt=commence).exclude(time__gt=cease))
for s10 in samples[::10]:
    # do something with every tenth...

This will pull all the samples from the database in one query, then only use 10% of them. You could also pull only the ones you need, one to a query:

sample_count = 600 # Somehow determine how many samples are in your time window.
samples = Weather.objects.filter().exclude(time__lt=commence).exclude(time__gt=cease)
for s_num in range(0, sample_count, 10):
    s = samples[s_num]
    # Now you have one sample in s.

To elaborate: range takes three arguments, start, stop, and step. For example:

>>> range(0, 100, 10)
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

In the second example, the range expression is used to create the index values of the samples we want. Then samples[s_num] evaluates the QuerySet to get just that one value from the database.

Leave a comment