djangosnippets.org: Latest snippets tagged with 'psycopg2'http://djangosnippets.org/tags/psycopg2/2011-03-02T18:25:55.175123-06:00Server Side Cursors for Django's psycopg2 Backend
2011-03-02T18:25:55.175123-06:00ryanbutterfieldhttp://djangosnippets.org/snippets/2375/<p>Based on discussion on <a href="http://thebuild.com/blog/2010/12/14/using-server-side-postgresql-cursors-in-django/">http://thebuild.com/blog/2010/12/14/using-server-side-postgresql-cursors-in-django/</a> and <a href="http://thebuild.com/blog/2010/12/13/very-large-result-sets-in-django-using-postgresql/">http://thebuild.com/blog/2010/12/13/very-large-result-sets-in-django-using-postgresql/</a> but instead implements them via extending the psycopg2 backend which allows you to use all of django's machinery without having to resort to using raw cursors.</p>
<p>Usage:</p>
<pre><code>qs = Model.objects.all()
with server_side_cursors(qs, itersize=100):
for item in qs.iterator():
item.value
if random_reason_to_break:
break
</code></pre>
<p>Setup:</p>
<p>In …</p>
Freely redistributable