Field allows null but not blank

If null is True and blank is False then validation becomes more complex.

If null is True and blank is False then Django and the database disagree on whether empty values are allowed.

The null and blank attributes are useful for optional fields. Both attributes default to False.

The null attribute specifies that the database will accept no value. The blank attribute specifies Django should not check if the value is present when Model.clean is called.

If Model.save is called via the shell without first calling Model.clean: the bad data will be saved and there may be unwanted side effects:

  • next time that record is validated it will fail
  • the datatype will be inconsistent
  • ORM queries will be more complex

These unwanted side effects can cause more serious problems that are hard to debug long after the original error occurred.

If we spot this issue in your GitHub pull request we give this advice:

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

If null is True and blank is False then validation becomes more complex.

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 field-null-not-blank in your pyproject.toml file.

Read more about configuring Django Doctor.