Skip to content

Instantly share code, notes, and snippets.

@vb64
Created July 21, 2022 19:23
Show Gist options
  • Select an option

  • Save vb64/ada42ba2b104e96b63260ce88fe61b0c to your computer and use it in GitHub Desktop.

Select an option

Save vb64/ada42ba2b104e96b63260ce88fe61b0c to your computer and use it in GitHub Desktop.
import io
import csv
def queryset2csv(qset):
"""Converts a Django queryset to the content of a csv file."""
out = io.StringIO()
writer = csv.writer(out, delimiter=';', lineterminator='\n')
for row in qset:
writer.writerow([row.field1, row.field2, ...])
content = out.getvalue()
out.close()
return content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment