ORM queries and Python code are made more complex because values will sometimes be None and other times str.

The null attribute specifies that the field can be None (null at database level). This is useful for optional fields.

This adds complexity as the data type is inconsistent: some instances may be strings and others None. This complicates ORM queries and extra code must guard against performing str operations on a NoneType.

This problem is avoided by specifying default='' instead. The database will even throw an exception if there is an attempt to explicitly save None, thus avoiding bad data.

If our GitHub code review bot spots this issue in your pull request it gives this advice:

django-doctorbotsuggested changes just now
models.py
1
+
class CommentModel(models.Model):
2
+
    body = models.TextField(null=True)
Suggested changes
-
    body = models.TextField(null=True)
+
    body = models.TextField(default="", blank=True)
Commit suggestion

ORM queries and Python code are made more complex because values will sometimes be None and other times str.

Read more
3
+
    date = models.DateField()
Update models.py
We're your Django code review copilot. Get code improvements right in your pull request with our GitHub code review bot.

Configuring this check

Code Review Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code nullable-string-field in your pyproject.toml file.

Read more about configuring Code Review Doctor.