Nullable string field

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 we spot this issue in your GitHub pull request we give this advice:

models.pymaintainabilitylow
class CommentModel(models.Model):
-
    body = models.TextField(null=True)
+
    body = models.TextField(default="", blank=True)
    date = models.DateField()

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

Read more
Protect your pull requests from over 40 types of common Django technical debts with our GitHub code review bot.

Configuring this check

Django 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 Django Doctor.