views:

622

answers:

2

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?

+5  A: 

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()
Daniel Roseman
Its still chewing through a ton of RAM when I use your call. :(
Paul Tarjan
It is the fact that the mysql query is buffered that is causing all the ram trouble.
Paul Tarjan
+1  A: 

You should consider saving your changes back to the database.

for alias in models.Alias.objects.all() :
    alias.update_points()
    alias.save()
S.Lott
well, update_points() calls save()
Paul Tarjan