我需要根据查询集结果生成一个 csv 文件,将生成的文件作为附件附加到电子邮件中并发送。如您所见,我需要遍历assigned_leads 并将其写入文件,因此我认为yield 可以解决问题。现在,当我运行代码时,我会收到带有附件的电子邮件,其中包含以下消息,而不是我期望的行。如果我使用 return 我从查询集结果中获取一行。
<generator object data at 0x7f5e508d93c0>
def send_lead_reminder(request):
usercompany = Listing.objects.filter(submitted_by=request.user)
assigned_leads = lead.objects.filter(assigned_to__in=usercompany).distinct()
def data():
csvfile=StringIO.StringIO()
csvwriter =csv.writer(csvfile)
for leads in assigned_leads:
csvwriter.writerow([leads.business_name, leads.first_name, leads.last_name, leads.email, leads.phone_number,leads.address, leads.city, leads.state, leads.zipcode, leads.submission_date, leads.time_frame, leads.comments])
yield csvfile.getvalue()
message = EmailMessage("Hello","Your Leads","myemail@gmail.com",["myemail@gmail.com"])
message.attach('invoice.csv', data(), 'text/csv')
#message.to="myemail@gmail.com"
message.send()
return HttpResponseRedirect('/')