如何将Django模型数据作为Excel文件附加并将其作为邮件发送给SMTP
我在Django模型中有一些数据,我想从该数据中创建Excel文件,并将其附加在SMTP中,并将其发送给目标用户。 我还使用django-import-export来导出excel文件。但是在这种情况下,我想在电子邮件中附加文件。
模型
class FinalBill(models.Model):
shipDate = models.DateField(blank = True, null = True, auto_now=False, auto_now_add=False)
customerReference = models.CharField(max_length = 200, blank = True, null = True)
customerConfirmation = models.CharField(max_length = 200, blank = True, null = True)
deliveryConfirmation = models.CharField(max_length = 200, blank = True, null = True)
address = models.CharField(max_length = 200, blank = True, null = True)
service = models.CharField(max_length = 200, blank = True, null = True)
weight = models.FloatField(blank = True, null = True)
pricingZone = models.CharField(max_length = 200, blank = True, null = True)
uspsCompRate = models.FloatField(blank = True, null = True)
charges = models.FloatField(blank = True, null = True)
surcharges = models.FloatField(blank = True, null = True)
totalSavings = models.FloatField(blank = True, null = True)
totalCharges = models.FloatField(blank = True, null = True)
customerID = models.CharField(max_length = 200)
is_exported = models.BooleanField(default=False)
exported_date = models.DateField(blank = True, null = True, auto_now=False, auto_now_add=False)
def __str__(self):
return str(self.deliveryConfirmation)
utills.py
def send_bill_on_mail(mailerID):
customerBill = FinalBill.objects.filter(customerID=mailerID, is_exported=False)
dataset = Bills().export(customerBill)
mail_subject = "Subject Name"
message = "Test Email Message"
to_email = "[email protected]"
file = "file"
mail = EmailMessage(mail_subject, message, settings.EMAIL_HOST_USER, [to_email])
mail.attach(file.name, file.read(), file.content_type)
mail.send()
i have some data in django models and i want to make excel file from that data and attach it in SMTP as file and send it to target user.
i am also using django-import-export to export the excel files .but in this case i want to attach the file in email.
Model
class FinalBill(models.Model):
shipDate = models.DateField(blank = True, null = True, auto_now=False, auto_now_add=False)
customerReference = models.CharField(max_length = 200, blank = True, null = True)
customerConfirmation = models.CharField(max_length = 200, blank = True, null = True)
deliveryConfirmation = models.CharField(max_length = 200, blank = True, null = True)
address = models.CharField(max_length = 200, blank = True, null = True)
service = models.CharField(max_length = 200, blank = True, null = True)
weight = models.FloatField(blank = True, null = True)
pricingZone = models.CharField(max_length = 200, blank = True, null = True)
uspsCompRate = models.FloatField(blank = True, null = True)
charges = models.FloatField(blank = True, null = True)
surcharges = models.FloatField(blank = True, null = True)
totalSavings = models.FloatField(blank = True, null = True)
totalCharges = models.FloatField(blank = True, null = True)
customerID = models.CharField(max_length = 200)
is_exported = models.BooleanField(default=False)
exported_date = models.DateField(blank = True, null = True, auto_now=False, auto_now_add=False)
def __str__(self):
return str(self.deliveryConfirmation)
Utills.py
def send_bill_on_mail(mailerID):
customerBill = FinalBill.objects.filter(customerID=mailerID, is_exported=False)
dataset = Bills().export(customerBill)
mail_subject = "Subject Name"
message = "Test Email Message"
to_email = "[email protected]"
file = "file"
mail = EmailMessage(mail_subject, message, settings.EMAIL_HOST_USER, [to_email])
mail.attach(file.name, file.read(), file.content_type)
mail.send()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
django-tables2可以在此帮助您帮助您,将表创建为类,该表将从数据中馈入,然后您可以将表导出到XLS文档,然后您可以通过电子邮件发送给用户。
参考:
django-tables2 can help you in this, you create your table as Class which will be fed from your data and then you can export the table to XLS document and then you can email to the user.
References: