Django-FileField检查是否无 [英] Django - FileField check if None

查看:109
本文介绍了Django-FileField检查是否无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有可选文件字段的模型

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"

此外, FieldFile 的布尔值将在没有文件时为False: bool(test.sound)== False 当没有$时c $ c> test.sound.name 是虚假的。

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

这篇关于Django-FileField检查是否无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆