def iter_fetchmany(cursor, size=100): """ Handles the awkwardness of iterating using fetchmany, and exposes all records as a (generated) iterable. Assumes django.db is the connection for the given cursor. FIXME for multi-db support. """ from django.db import connection rowset_iter = iter((lambda: cursor.fetchmany(size)), connection.features.empty_fetchmany_value) for rows in rowset_iter: for row in rows: yield row