2012-01-13 9 views
38

मैं एक वैकल्पिक फ़ाइल क्षेत्र के साथ एक मॉडल हैDjango - FileField जांच करता है, तो कोई भी

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

की मुझे लगता है कि क्यों मिलता है एक मूल्य

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

रखते हैं?

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

मैं कैसे जांच कर सकते हैं एक फाइल है अगर वहाँ सेट? bool(test.sound) == False जब test.sound.name falsy है:

उत्तर

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

इसके अलावा, FieldFile के बूलियन मान झूठी जब कोई फ़ाइल है किया जाएगा।

+0

कौन सा बूलियन मूल्य? क्या आपका मतलब है 'test.sound गलत है'? –

+0

नहीं 'है' - test.sound में 'गलत' जैसी ही पहचान नहीं होगी। हालांकि, 'test.ound: प्रिंट करें "हां" ', अन्यथा: प्रिंट करें" नहीं "' प्रिंट नहीं करना चाहिए। 'बूल (test.sound)' 'गलत 'होना चाहिए। – AdamKG

+11

+1: 'bool (fieldFile) '' गलत है' जब 'फ़ील्डफाइल.नाम' गलत है। – DzinX

संबंधित मुद्दे