W3docs

What is the difference between null=True and blank=True in Django?

In Django, null=True and blank=True are both used to specify options for fields in a model.

In Django, null=True and blank=True control how fields handle empty values, but they operate at different layers.

null=True specifies that the field can be stored as NULL in the database. This is strictly a database-level setting. For example:

Use null=True in Django - Python

class MyModel(models.Model):
    my_field = models.CharField(max_length=255, null=True)

blank=True specifies that the field can be left empty during form validation. If set to True, the field is allowed to be blank in forms and the Django admin. For example:

Use blank=True in Django - Python

class MyModel(models.Model):
    my_field = models.CharField(max_length=255, blank=True)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

Note that blank=True does not imply null=True. For CharField and TextField, Django stores empty strings as '' in the database, so null=True is unnecessary and generally discouraged. For other field types (such as IntegerField or ForeignKey), you typically need to set both blank=True and null=True if you want to allow empty values in forms and NULL in the database. See the official Django documentation for more details.

Use blank=True and null=True simultaneously in Django - Python

class MyModel(models.Model):
    my_field = models.CharField(max_length=255, blank=True, null=True)

In this example, my_field can be left blank in forms and stored as NULL in the database.