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

后端 未结 4 971
花落未央
花落未央 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:15

    When in doubt, check the code. Here's an excerpt from django.db.models.fields.files:

    def open(self, mode='rb'):
        self._require_file()
        self.file.open(mode)
    # open() doesn't alter the file's contents, but it does reset the pointer
    open.alters_data = True
    

    So, in the case of a FileField, open reopens the file using the specified mode. Then, once you call open, you can continue to use methods like read using the newly applied mode.

提交回复
热议问题