The null
attribute specifies that the field can be None
, or null
at database level.
This makes the ORM queries and the Python code more complex because the values will sometime be None
and other times, a string
.
Hence, you'd need extra code to check on performing string operations on a NoneType
.
It is recommended to provide a default=''
option to the fields instead.
This way, you wonn't have to worry about null values and the database would also throw an exception if there is an attempt to explicitly save None
.
class Customer(models.Model):
remarks = models.CharField(null=True)
query = Q(remarks__isnull=True) | Q(remarks='')
for customer in Customer.objects.filter(query):
...
class Customer(models.Model):
remarks = models.CharField(default='')
for customer in Customer.objects.filter(remarks=''):
# customer.remarks is always a str
...