[Fixed]-Reading InMemoryUploadedFile twice

22👍

You should be able to call seek(0) on the underlying file object:

my_file_obj.file.seek(0)

8👍

Just adding to @Daniel Roseman answer.

Or you could just call my_file_obj.open() .
It does the same thing.

Here is the code from Django’s docs

    class InMemoryUploadedFile(UploadedFile):
        def open(self, mode=None):
            self.file.seek(0)
            return self

reference https://docs.djangoproject.com/en/3.0/_modules/django/core/files/uploadedfile/

👤FYP

0👍

I am doing a write operation on s3 from a file source InMemoryUploadedFile. Here is the catch the file buffer i am giving to write on s3 object, it closes the file after reading. If i am to seek but it is non seekable object.

https://github.com/boto/boto3/issues/256

so in python there is a deepcopy i made a copy of object, now i have two object with diffrent buffer reader

file_obj1 = copy.deepcopy(file_obj)
buffer1 = file_obj.file
buffer2 = file_obj1.file

Here file_obj is a InMemoryUploadedFile

Leave a comment