Python django正在加载数百万行
所以我需要使用django遍历数百万行。这与django从DB抓取它们的方式不符,所以我研究了一下 我尝试了github建议的确切代码:Python django正在加载数百万行,python,django,Python,Django,所以我需要使用django遍历数百万行。这与django从DB抓取它们的方式不符,所以我研究了一下 我尝试了github建议的确切代码: from djorm_core.postgresql import server_side_cursors with server_side_cursors(): for item in MyModel.objects.all(): #do something 我得到了以下错误: 我无法显示错误开始的实际行,但它是在myModel.
from djorm_core.postgresql import server_side_cursors
with server_side_cursors():
for item in MyModel.objects.all():
#do something
我得到了以下错误:
我无法显示错误开始的实际行,但它是在myModel.objects.all中对应的for项上开始的
有没有其他方法可以做到这一点?有没有办法继续使用此模块,或者它只是被破坏了,因为这正是github帐户所建议的?您是否尝试使用django的原始sql?是否尝试使用objects.filter….iterator,将DEBUG设置为False?我没有尝试原始sql,我将使用什么sql?迭代器将在执行queryset时立即返回所有行,然后再对其进行分块,并给出内存不足的条件。
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in __iter__(self)
94 - Responsible for turning the rows into model objects.
95 """
---> 96 self._fetch_all()
97 return iter(self._result_cache)
98
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in _fetch_all(self)
852 def _fetch_all(self):
853 if self._result_cache is None:
--> 854 self._result_cache = list(self.iterator())
855 if self._prefetch_related_lookups and not self._prefetch_done:
856 self._prefetch_related_objects()
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in iterator(self)
941 names = extra_names + field_names + aggregate_names
942
--> 943 for row in self.query.get_compiler(self.db).results_iter():
944 yield dict(zip(names, row))
945
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in results_iter(self)
707 fields = None
708 has_aggregate_select = bool(self.query.aggregate_select)
--> 709 for rows in self.execute_sql(MULTI):
710 for row in rows:
711 if has_aggregate_select:
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
780
781 cursor = self.connection.cursor()
--> 782 cursor.execute(sql, params)
783
784 if not result_type:
/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
67 start = time()
68 try:
---> 69 return super(CursorDebugWrapper, self).execute(sql, params)
70 finally:
71 stop = time()
/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
51 return self.cursor.execute(sql)
52 else:
---> 53 return self.cursor.execute(sql, params)
54
55 def executemany(self, sql, param_list):
/usr/local/lib/python2.7/dist-packages/django/db/utils.pyc in __exit__(self, exc_type, exc_value, traceback)
97 if dj_exc_type not in (DataError, IntegrityError):
98 self.wrapper.errors_occurred = True
---> 99 six.reraise(dj_exc_type, dj_exc_value, traceback)
100
101 def __call__(self, func):
/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
51 return self.cursor.execute(sql)
52 else:
---> 53 return self.cursor.execute(sql, params)
54
55 def executemany(self, sql, param_list):
ProgrammingError: can't use a named cursor outside of transactions