我有一个具有以下内容的charfield:
myString = models.CharField(max_length = 50,null = True,blank = True)在我的许多对象中,当通过django管理界面。在MySQL中,该列为VARCHAR(50),默认值为NULL。
无论我尝试在自己的views.py中执行什么测试以检测空值,似乎总是在评估判断条目是否为空为否:
myString为None myString == len(myString)== 0如何区分该字段的空白值和非空白值?
谢谢
编辑:
实际视图如下,仅当此字段不为空时,我才想执行块。
如果myObject.myString:#做某事该块始终执行,并且我尝试了上述所有测试示例一个空白字段。 :确实有效。
解决方案如果您的 d 是 None 或 k-
如果d:#做某事其他:#做其他
I have a charfield with the following:
myString = models.CharField(max_length=50,null=True,blank=True)In many of my objects, this particular string is often left blank, when created through the django admin interface. In MySQL, the column is VARCHAR(50) with default=NULL.
Whatever test I try to do in my views.py to detect blank values, always seems to evaluate to false whether the entry is blank or not:
myString is None myString=="" len(myString)==0How can I discriminate between blank and non-blank values for this field?
Thanks
EDIT:
Actual view is the following, I want to execute a block only if this field is not blank.
if myObject.myString: #do somethingthe block always executes, and I have tried all the above examples of testing for a blank field.
EDIT 2:
Scratch that, if myObject.myString: does indeed work.
解决方案if your d is either None or "" then simply check -
if d: #do something else: #do something else
更多推荐
在Django中测试空/空字符串
发布评论