Django find_each(像RoR)

时间:2014-10-03 20:02:19

标签: python ruby-on-rails django

有没有办法在django中使用find_each

根据rails文档:

  

此方法仅用于批量处理大型   一次不能满足记忆的记录数量。如果你   只需要循环不到1000条记录,它可能更好   只是为了使用常规的查找方法。

http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_each

感谢。

2 个答案:

答案 0 :(得分:3)

一种可能的解决方案可能是使用内置的“Paginator”类(可以省去很多麻烦)。

https://docs.djangoproject.com/en/dev/topics/pagination/

尝试类似:

from django.core.paginator import Paginator
from yourapp.models import YourModel

result_query = YourModel.objects.filter(<your find conditions>)

paginator = Paginator(result_query, 1000) # the desired batch size

for page in range(1, paginator.num_pages):
   for row in paginator.page(page).objects_list:
       # here you can add your required code

或者,您可以根据需要使用限制选项(https://docs.djangoproject.com/en/dev/topics/db/queries/#limiting-querysets)来迭代结果。

答案 1 :(得分:0)

您可以使用循环和切片查询集来查询整个表的部分内容。

如果您正在使用Debug = True,那么在每次循环后刷新查询很重要,因为这会导致内存问题(Django会存储在脚本完成或终止之前运行的所有查询)

如果您需要限制查询集的结果,可以替换&#34; .all()&#34;使用适当的&#34; .filter(条件)&#34;

from django import db
from myapp import MyModel

# Getting the total of records in the table
total_count = MyModel.objects.all().count()
chunk_size = 1000 # You can change this to any amount you can keep in memory
total_checked = 0

while total_checked < total_count:
    # Querying all the objects and slicing only the part you need to work
    # with at the moment (only that part will be loaded into memory)
    query_set = MyModel.objects.all()[total_checked:total_checked + chunk_size]

    for item in query_set:
        # Do what you need to do with your results
        pass

    total_checked += chunk_size

    # Clearing django's query cache to avoid a memory leak
    db.reset_queries()