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:
These unwanted side effects can cause more serious problems that are hard to debug long after the original error occurred.
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | class CommentModel(models.Model): | |
2 | + | body = models.TextField() | |
3 | + | date = models.DateField(null=True) |
If null is True
and blank is False
then validation becomes more complex.
- | date = models.DateField(null=True) |
+ | date = models.DateField(null=True, blank=True) |