I have written a function that turns a queryset into a CSV file, that is then emailed to the user. Since there is no need to store the file on the system I've decided to use Tempfile objects. However though I am succesfull in writing to these objects, I am unable to read them - which is preventing me from emailing them as attachment
columns = instances[0].get_columns
# create tempfile, in TEXT (t) mode so as not to trip up the
# csv module, which cant handle binary data.
file = tempfile.NamedTemporaryFile(mode='wt')
try:
writer = csv.writer(file)
writer.writerow([field[0] for field in columns])
for instance in instances:
row = [getattr(instance, str(field[0])) for field in columns]
# email the file
body = 'The exported file has been included as a attachment.'
email = EmailMessage(
subject='CSV Export',
from_email='******',
to=['*****'],
body=body,
)
email.attach('export.csv', file, 'text/csv')
email.send()
This triggers the following error: '_io.TextIOWrapper' object has no attribute 'splitlines'
After some googling it seemed that I had to .read()
the file
however, the new code (email.attach('export.csv', file.read(), 'text/csv')
UnsuportedOperation: not readable` error.
Other posts suggest that, besides .read() I needed to 'rewind' the file using file.seek(0, os.SEEK_END)
, but the not readable error
keeps triggering.
If I remove the file.read()
but keep the file.seek(0, os.SEEK_END)
which leads to a return of the '_io.TextIOWrapper' object has no attribute 'splitlines'
error.
Does anyone know what I am doing wrong?