def queryset_iterator(queryset, chunksize=1000):
    try:
        last_pk = queryset.order_by('-pk')[0].pk
    except IndexError:
        return

    queryset = queryset.order_by('pk')
    pk = queryset[0].pk - 1
    while pk < last_pk:
        for row in queryset.filter(pk__gt=pk)[:chunksize]:
            pk = row.pk
            yield row
        gc.collect()
dataset/ETHPy150Open jaddison/django-simple-elasticsearch/simple_elasticsearch/utils.py/queryset_iterator/original