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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With