Django FileField (or ImageField) open() method returns None for valid file?

后端 未结 4 972
花落未央
花落未央 2021-02-02 08:42

let me put it like this:

model.py:

class Task(models.Model):
    ...
    seq_file = models.FileField(upload_to=\'files/\', blank=True, null=True)
    ...         


        
4条回答
  •  花落未央
    2021-02-02 09:20

    A FileField will give you a file-like object and there is no need to call open() on it. In your example, just call task.seq_file.file.

    Why is that? There are many storage backends for FileField, and many of them are not backed by a file in disk (think of S3 storage, for example). I guess that is why the documentation says it returns a file-like object, not a file. For some kinds of storage the "open" method makes no sense.

提交回复
热议问题