开发者

Django check to see if field is blank?

I retrieved an object from my database. This object has a foreign key field, with the attribute blank=True. How can I check 开发者_如何学Goto see if it was actually left blank?

Thanks for the help!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜