The desired outcome is to export a spreadsheet of all users in the database, but rather than export them all at once - break up the users into 3 groups and export each of these groups as a spread sheet.
I am using xlsxwriter and StringIO
Thus far the code is returning only the first HttpResponse object out of the 3 (aka the first chunk of users). I tried using StreamingHttpResponse but believe I misapplied it/it wasn't appropriate for this task.
The question/s: Can I return multiple spreadsheets in one response? If not, would the best thing to do be to call this function multiple times, once for each user group? (edited after first comment)
Thanks Much!
Code below:
def export_users(request):
# all users
users = User.objects.all()
# breaks up all users into 3 groups
user_groups = [users[x:x+2] for x in xrange(0, len(users), 2)]
# FUNCTIONALITY NOTES --> idea is to split number of users for exporting into groups of 3 then export each group. right now it stops after the first group
# list var for storing all responses
full_response = []
for group in user_groups:
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=UserReport.xlsx'
# user group
print "group! ---> %s"%(group)
# creates data variable to write to response
xlsx_data = WriteToExcel(group)
response.write(xlsx_data)
# appending each response to an array
full_response.append(response)
print len(full_response)
# all response objects
print full_response
# returning one here
return response
# non-functioning attempt to return all responses
# for response in full_response:
# print response
# return response