Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - FileField check if None

I have a model with an optional file field

class MyModel(models.Model):   name = models.CharField(max_length=50)   sound = models.FileField(upload_to='audio/', blank=True) 

Let's put a value

>>> test = MyModel(name='machin') >>> test.save() 

Why do I get that ?

>>> test.sound <FieldFile: None> >>> test.sound is None False 

How can I check if there is a file set ?

like image 222
Pierre de LESPINAY Avatar asked Jan 13 '12 12:01

Pierre de LESPINAY


1 Answers

if test.sound.name:       print "I have a sound file" else:         print "no sound" 

Also, FieldFile's boolean value will be False when there's no file: bool(test.sound) == False when test.sound.name is falsy.

like image 66
AdamKG Avatar answered Oct 23 '22 07:10

AdamKG