Django check to see if field is blank?

前端 未结 1 2011
夕颜
夕颜 2021-02-01 15:22

I retrieved an object from my database. This object has a foreign key field, with the attribute blank=True. How can I check to see if it was actually left blank?

Thanks

相关标签:
1条回答
  • 2021-02-01 15:42

    blank=True is just telling the admin site that the field can be left blank.

    Unless you set null=True as well, your database should complain if you tried to enter a blank value.

    If your foreign key field can take null values, it will return None on null, so to check if it was "left blank", you could simply check if the field is None.

    >>>   obj.foreignkeyfield is None
    True
    
    
    if not obj.foreignkeyfield:
        print "This field was left blank"
    
    0 讨论(0)
提交回复
热议问题