python - Iterating over a Django QuerySet while deleting objects in the same QuerySet -


i wondering best way iterate on django queryset while deleting objects within queryset? example, have log table entries @ specific times, , wanted archive them there no more 1 entry every 5 minutes. know may wrong, kind of going for:

toarchive = log.objects.all().order_by("-date") start = toarchive[0].date interval = start - datetime.timedelta(minutes=5) entry in toarchive[1:]:             if entry.date > interval:         entry.delete()     else:         interval = entry.date - datetime.timedelta(minutes=5) 

so guess have answered own question asking it, if else curious. thought there have been problem when deleting objects while iterating on them, there isn't. code snippet in question right way it.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -