[Fixed]-Reading InMemoryUploadedFile twice


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



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):
            return self

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



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.


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