无法将文件附件保存到 Django 模型文件字段中
Unable to save fileattachment into Django Model FileField
我目前正在将交换服务器电子邮件集成到我的应用程序中。我可以使用 exchangelib 从我的电子邮件中检索附件。我正在尝试将附件保存到我的 Django 模型文件字段中。但是,根据我尝试的不同解决方案,它无法处理不同的错误。任何帮助表示赞赏。谢谢你。下面是我的一些代码:
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()
请查看以下代码片段并尝试在您的代码中执行相同的操作
from django.core.files.base import ContentFile
...
saveattachments = Attachments(name=attachment.name)
saveattachments.attachment.save(attachment.name, ContentFile(attachment.content))
saveattachments.save()
我目前正在将交换服务器电子邮件集成到我的应用程序中。我可以使用 exchangelib 从我的电子邮件中检索附件。我正在尝试将附件保存到我的 Django 模型文件字段中。但是,根据我尝试的不同解决方案,它无法处理不同的错误。任何帮助表示赞赏。谢谢你。下面是我的一些代码:
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()
请查看以下代码片段并尝试在您的代码中执行相同的操作
from django.core.files.base import ContentFile
...
saveattachments = Attachments(name=attachment.name)
saveattachments.attachment.save(attachment.name, ContentFile(attachment.content))
saveattachments.save()