Sending EMail with Attach file in Django Rest Framework

Please I need help in solving this.
The “read” method on the doc object shows this error:
ValueError: I/O operation on closed file.

def send_comment_mail_notification(request, space, target_email):
    message = request.POST.get('description', '')
    subject = request.POST.get('subject', '')
    from_mail = space.email
    to = target_email
    msg = EmailMessage(subject, message, from_mail, [to],)
    doc = request.FILES['document']
    print(doc.read())
    msg.attach(doc.name, doc.read(), doc.content_type)
    msg.content_subtype = "html"
    msg.send()
class CommentViewSet(BaseViewSet):
    permission_classes = (BelongToSpace, )
    serializer_class = serializers.CommentSerializer
    parser_classes = (MultiPartParser, FormParser, FileUploadParser)
    queryset = models.Comment.objects.all()

    def callback(self, **kwargs):
        return send_comment_mail_notification(self.request, self.space, "aa@gmail.com")

You can’t read the file twice. Once you’ve read it, it’s been consumed. You can read it and save the data in a variable, then print it and email it.

I removed the print(doc.read()), but I still get the same error.
Someone suggested that the file has been read in the serializer before the send_notification function which is also reading the file. Pls,how do I make the file not to read from the serializer.

I don’t know enough about DRF to answer that question, but my assumption would be that the serializer would have read the file and saved the data somewhere. (I’ve never tried to handle an uploaded file through DRF.)

The FileUploadParser docs gives me the impression that the file data gets put into the request.data attribute. I’d try examining that to see if it has the data you’re looking for.

I will be glad. Thank you so much