Python – Django Model returning NoneType

djangodjango-viewspython

I have a model Product

it has two fields size & colours among others

colours = models.CharField(blank=True, null=True, max_length=500)
size = models.CharField(blank=True, null=True, max_length=500)

In my view I have

current_product = Product.objects.get(slug=title)
if len(current_product.size) != 0 :
    current_product.size = current_product.size.split(",")

and get this error:

object of type 'NoneType' has no len()

What is NoneType and how can I test for it?

Best Solution

NoneType is the type that the None value has. You want to change the second snippet to

if current_product.size: # This will evaluate as false if size is None or len(size) == 0.
  blah blah
Related Question