2012-01-13 16 views
38

FileField onayDjango - Ben isteğe bağlı dosya alanına sahip bir modele sahip Yok

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

bunu alırım Neden en

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

bir değer koyalım olur?

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

Nasıl bir dosya varsa kümesi olup olmadığını kontrol edebilir? bool(test.sound) == Falsetest.sound.name falsy: hiçbir dosya varken

cevap

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

Ayrıca FieldFile 'ın boole değeri False olacaktır.

+0

Hangi boole değeri? 'Test.sound false' demek istiyor musunuz? –

+0

"değil" - test.sound "False" ile aynı kimliğe sahip olmayacak. Ancak, eğer test.sound: print "yes" ',' else: print "no" 'yazdırması gerekir. boole (test.sound) 'false olmalıdır. – AdamKG

+11

+1: 'bool (fieldFile)', "fieldFile.name" false olduğunda 'false'dir. – DzinX

İlgili konular