Django : Iterate over a query set without cache

2019-03-29 12:20发布

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?

2条回答
2楼-- · 2019-03-29 13:03

You should consider saving your changes back to the database.

for alias in models.Alias.objects.all() :
    alias.update_points()
    alias.save()
查看更多
对你真心纯属浪费
3楼-- · 2019-03-29 13:20

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()
查看更多
登录 后发表回答