[Answered ]-Empty CharField with null=True returns NoneType Django 1.8

2👍

As others have pointed out, null doesn’t exist in python, None is the way to represent null values. That said, there are several ways you could address your issue:

Option 1: Change your field definition to:

 number_of_likes = models.IntegerField(default=0)

Option 2: If you don’t want to change the field, you could:

 object.number_of_likes = int(object.number_of_likes) + 1 if object.number_of_likes else 1

Hope this helps

0👍

Just to check I did a python manage.py shell and typed

>>> null

and got:

NameError: name 'null' is not defined

I don’t think there’s a null, except when a None is stored into the database.

0👍

null may not have public availabilty but exists as a property when using models.Integerfield although Django docs has explicitly said that it is best to avoid using it with Charfield – https://docs.djangoproject.com/en/4.0/ref/models/fields/

Leave a comment