Missing
blank
kwarg (defaults to False
) will allow field to be null but not blank 84class SupportStatus(models.Model):
85 name = models.CharField(max_length=150)
86 secret = models.CharField(max_length=400)
87 expiry = models.DateTimeField(db_index=True, null=True) 88 in_limits = models.BooleanField(default=True)
89 discoverable = models.BooleanField(default=False)
90 limits = models.JSONField(default=dict)
Missing
blank
kwarg (defaults to False
) will allow field to be null but not blank506 relation = models.IntegerField()
507 secondary = models.IntegerField(default=0)
508 changes = models.IntegerField()
509 data = models.JSONField(null=True)510
511 objects = MetricManager.from_queryset(MetricQuerySet)()
512
Missing
blank
kwarg (defaults to False
) will allow field to be null but not blank32 settings.AUTH_USER_MODEL, unique=True, on_delete=models.deletion.CASCADE
33 )
34 tos = models.DateField(default=date(1970, 1, 1))
35 address = models.GenericIPAddressField(null=True)36 user_agent = models.CharField(max_length=200, default="")
37 timestamp = models.DateTimeField(auto_now=True)
38
Missing
blank
kwarg (defaults to False
) will allow field to be null but not blank 781 project = models.ForeignKey(
782 Project, verbose_name=gettext_lazy("Project"), on_delete=models.deletion.CASCADE
783 )
784 expiry = models.DateTimeField(gettext_lazy("Block expiry"), null=True) 785
786 class Meta:
787 verbose_name = "Blocked user"
Missing
blank
kwarg (defaults to False
) will allow field to be null but not blank270 db_index=True,
271 )
272 params = models.JSONField(default=dict)
273 address = models.GenericIPAddressField(null=True)274 user_agent = models.CharField(max_length=200, default="")
275 timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
276
Description
If null
is True
and blank
is False
then Django and the database disagree on whether empty values are allowed.
The null
attribute specifies that the database will accept it if no value is passed to the field. 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, such as:
- Complex ORM queries
- Validation error if the records are validated the next time
- Inconsistent datatypes
These unwanted side effects can cause more serious problems that are hard to debug long after the original error occurred.
Not preferred:
class Feedback(models.Model):
created = models.DateField(null=True)
updated = models.DateField(null=True, blank=False)
Preferred:
class Feedback(models.Model):
created = models.DateField(null=True, blank=True)
updated = models.DateField(null=True, blank=True)