[Answered ]-Django FileField saving empty file to database

1👍

I found out that fp.read() throws content based on the current pointer in the file. At least, that is my understanding. So after I dump assets dict as json the to temp file, I have to bring back the cursor to the beggining using fp.seek(0). This way, when I call fp.read() inside file=ContentFile(fp.read(), …) it actually reads all the content. It was giving me empty because there was nothing to read since the cursor was at the end of the file.

    fp = tempfile.TemporaryFile(mode="w+")
    json.dump(assets, fp)
    fp.flush()
    fp.seek(0) // important

    CollectionSnapshot.objects.create // stays the same

Leave a comment