Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Iterate over a query set without cache

I have a dumb simple loop

for alias in models.Alias.objects.all() :
    alias.update_points()

but looking into the django QuerySet it seems to keep around a _result_cache of all the previous results. This is eating Gigs and Gigs of my machine and eventually everything blows up.

How can I throw away all the stuff that I won't ever care about?

like image 998
Paul Tarjan Avatar asked Sep 18 '09 08:09

Paul Tarjan


1 Answers

Use the queryset's iterator() method to return the models in chunks, without populating the result cache:

for alias in models.Alias.objects.iterator() :
    alias.update_points()
like image 71
Daniel Roseman Avatar answered Sep 30 '22 21:09

Daniel Roseman