Django检查字段是否为空白? [英] Django check to see if field is blank?
本文介绍了Django检查字段是否为空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢您的帮助!
解决方案
blank = True
只是告诉管理员网站,该字段可以留空。
除非您设置 null = True
,否则您的数据库应该投诉,如果您尝试输入一个空白值。
如果您的外键字段可以取空值,它将返回None,否则为空,以检查是否为空白,您只需检查该字段是否为无
。
>>> obj.foreignkeyfield是无
True
如果没有obj.foreignkeyfield:
print该字段留空
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 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"
这篇关于Django检查字段是否为空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文