How to check if Django FileField is empty?

What is the correct way to check if Django FileField is empty, i.e. when no file was downloaded?

It seems like the attribute name u''is when the field is empty, but I do not know if it is reliable.

+3
source share
2 answers

I ran into a similar problem and found a possible solution (of course, not the best). I am currently checking if the cleared data inside the file field is an instance of the TemporaryUploadedFile class (django.core.files.uploadedfile.TemporaryUploadedFile):

** your code is here **

from django.core.files.uploadedfile import TemporaryUploadedFile

** your code is here **

if isinstance (form_instance.cleaned_data ['my_file'], TemporaryUploadedFile):   # do stuff

, .

!

+2

python, :

if some_object.file:
    # File exists!
    # This of course does not guarantees that file exists on disk, or that it is 
    # readable.
else:
    # No file.
-1

Source: https://habr.com/ru/post/1771150/


All Articles