[Django]-Django how to open file in FileField

7👍

✅

The .open() opens the file cursor but does not return it, since it depends of your storage (filesystem, S3, FTP…). Once opened, you can use .read() to iterate over the file content.

stocklist.csv_file.open(mode="rb")
content = stocklist.csv_file.read()
stocklist.csv_file.close()

If you want to specifically work with file descriptor then you can use your storage functionality:

from django.core.files.storage import DefaultStorage
storage = DefaultStorage()
f = storage.open(stocklist.csv_file.name, mode='rb')

Leave a comment