meepmeep meepmeep - 2 years ago 132
Python Question

testing for empty/null string in django

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 to detect blank values, always seems to evaluate to false whether the entry is blank or not:

myString is None

How can I discriminate between blank and non-blank values for this field?



Actual view is the following, I want to execute a block only if this field is not blank.

if myObject.myString:
#do something

the block always executes, and I have tried all the above examples of testing for a blank field.


Scratch that,
if myObject.myString:
does indeed work.

Answer Source

if your d is either None or "" then simply check -

if d: 
    #do something
    #do something else
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download