0

I am currently integrating exchange server email to my application. I am able to retrieve attachments from my emails using exchangelib. I am trying to save the attachments into my Django model filefield. However, it is not working with different errors based on different solutions I tried. Any help is appreciated. Thank you. Below are some of my code:

models.py

class Attachments(models.Model):
    name = models.CharField(max_length=255)
    attachment = models.FileField(upload_to="attachments/")

views.py

for attachment in item.attachments:
        if isinstance(attachment, FileAttachment):
            attachmentlist.append(attachment.name)
            saveattachments = Attachments(
                name=attachment.name,
                attachment=(attachment.content)
                )
            saveattachments.save()

1 Answers1

2

Please look an following snippet and try to do same in you code

from django.core.files.base import ContentFile
...
saveattachments = Attachments(name=attachment.name)
saveattachments.attachment.save(attachment.name, ContentFile(attachment.content))
saveattachments.save()
LinnTroll
  • 705
  • 4
  • 10