[Django]-Django – FileField check if None

124👍

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

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

👤AdamKG

0👍

According to this answer from a different question, you can try this:

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

def __nonzero__(self):
    return bool(self.sound)

Leave a comment