I am wondering what is the best way to iterate over a Django QuerySet when deleting objects in Queryset? For example, let's say that you have a table of logs with records at a specific time, and you want to archive them so that no more than 1 record every 5 minutes. I know this may be wrong, but this is what I am going to do:
toarchive = Log.objects.all().order_by("-date") start = toarchive[0].date interval = start - datetime.timedelta(minutes=5) for entry in toarchive[1:]: if entry.date > interval: entry.delete() else: interval = entry.date - datetime.timedelta(minutes=5)
user1630866
source share