在django中有没有办法使用find_each
?Django find_each(像RoR)
根据轨道文档:
此方法仅用于使用的大 量,将不适合在内存中一次全部记录批处理。如果你只需要循环少于1000条记录,那么只需使用常规查找方法就可以更好地使用 。
http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_each
感谢。
在django中有没有办法使用find_each
?Django find_each(像RoR)
根据轨道文档:
此方法仅用于使用的大 量,将不适合在内存中一次全部记录批处理。如果你只需要循环少于1000条记录,那么只需使用常规查找方法就可以更好地使用 。
http://apidock.com/rails/ActiveRecord/Batches/ClassMethods/find_each
感谢。
一个可能的解决方案可能是使用内置的“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)按需要遍历结果。
您可以使用循环和切片查询集来查询整个表的各个部分。
如果你使用调试工作= TRUE,你刷新你的查询每个循环之后,因为这可能会导致内存问题(Django的门店,这些门店运行,直到脚本完成或死亡的所有查询)是很重要的。
如果需要限制查询集的结果,你可以更换“所有()”与相应的“.filter(条件)”
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()
这无疑解决了这个问题。但是这应该为每个模型完成。在红宝石中,有一个通用的方法来实现这一点。 – 2014-10-05 18:53:25