How to check ImageField is empty

前端 未结 1 824
我寻月下人不归
我寻月下人不归 2020-12-30 19:21

I have an ImageField in my model and when I\'m saving it I want to check that if it\'s None or not.

In django shell I\'m calling my object\'s ImageField and it gives

相关标签:
1条回答
  • 2020-12-30 19:45

    I found that the ImageField's name is u'', so is there any better way to do it ?

    Actually, it looks like that's exactly how this class evaluates bool(), so the better way is to just test its bool() by calling if p.avatar

    ImageFieldFile subclasses File, which defines:

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

    So the better way is indeed:

    if not p.avatar:
       print "I don't exist"
    
    bool(p.avatar) is False
    
    0 讨论(0)
提交回复
热议问题