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

后端 未结 4 965
花落未央
花落未央 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条回答
  •  梦毁少年i
    2021-02-02 09:31

    Surprisingly but django.db.models.fields.files does not utilize file.storage.exists() method so I had to implement my own small function to have cross-storage compatible check for actual physical file existence:

    # Check whether actual file of FileField exists (is not deleted / moved out).
    def file_exists(obj):
        return obj.storage.exists(obj.name)
    

提交回复
热议问题