[Django] Pass iterator to response

Sometimes it may be more efficient to transmit response while it being generated.


Before use this, notice possible performance issues:

Small example:

def my_iterator():
    yield 'Hi!\n'
    for i in MyModel.objects.all():
        yield '%s\n' % i.field
    yield 'By!\n'

def my_view(request):
    response = HttpResponse(my_iterator(), content_type='text/plain')
    response['Content-Disposition'] = 'attachment; filename=sometext.txt'
    return response
Licensed under CC BY-SA 3.0