from io import StringIO
import csv


def export_csv(items, fields):
    """
    Generate the data with csv.writer and stream the response. 
    Use StringIO to write to an in-memory buffer rather than 
    generating an intermediate file.
    """
    data = StringIO()
    w = csv.writer(data)
    w.writerow(fields)  # header
    for i in items:
        w.writerow([getattr(i, field) for field in fields])
    return data.getvalue()