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

Key takeaways:

  1. null=True allows a field in the database to store NULL values, enabling the database column to remain empty.

  2. blank=True permits form fields to be left empty during validation, making them optional for users to fill out.

  3. Both null=True and blank=True are used to control the behavior of fields at different levels: database vs. form validation.

  4. An exception exists for CharField and TextField, which handle empty values by storing None or an empty string instead of NULL.

In Django, the terms null and blank have distinct meanings and can be used differently when declaring fields in a model. null controls the field's validation on the database level, while blank is used for form validation at the application level.

Let's examine the following Django model:

class Employee(models.Model):
#Employee fields include name, id, date of Birth and occupation
name = models.CharField(max_length=100)
emp_id = models.CharField(max_length=100)
dob = models.DateField()
occupation = models.TextField()

We cannot omit any field when creating an Employee from this model, since all of them are required by default. This is where the null and blank keywords come into play.

The null argument

The null argument is used to specify whether or not the database column corresponding to a field should permit NULL values. For the example above, we can make the following adjustments to make the employee's dob field optional in a database:

dob = models.DateField(null=True)

By adding null=True for a dob field, our database can store an employee without mentioning any date of birth. The dob column for our model's database can have NULL (or empty) values stored in it.

Note: In order to update the database schema to set values as NULL or NOT NULL, make sure to build a new migration and apply it accordingly.

The blank argument

The blank argument is used to specify whether or not a field is allowed to be left empty in formsAny user interface component that is used to collect and submit data from users, like a sign-up page in any web application.. To make the dob field optional in forms, we will make the following changes:

dob = models.DateField(blank=True)

By adding blank=True, we are specifying that dob in field validation can be empty. So, if a user (e.g. admin) fills the entire form with except the date of birth, our field validation will permit this entry.

Question

Can the database permit the insertion of empty or NULL values in the dob field via the following code snippet: dob = models.DateField(blank=True)?

Show Answer

To allow insertion of NULL (or empty) values at both the field and database validation for dob, we can make the following changes:

dob = models.DateField(blank=True, null=True)

Exception

There is an exception to the rules mentioned above when using CharField or TextField in our model. In these cases, Django's syntax avoids the need for null=True. Both these fields handle empty data by storing either None or an empty string "" as appropriate values.

#.../
emp_id = models.CharField(max_length=100, blank=True)
#.../

Code example

The following is the coding example for null=True and blank=True. Try removing null=True from dob field on line 6 in djangodb/website/models.py and you will get an integrity error.

Note: Upon launching the application, you will encounter a form. Please complete all the fields in the form, excluding the dob field. Observe the behavior of the code both before and after the null=True statement is removed from the code.

Bud1�godbdscldjangodbdsclboolwebsitedsclbool @� @� @� @E�DSDB `� @� @� @
An example of null=True and blank=True

Code explanation

In the Python models.py file:

  • Line 1: We import the models module from the django.db package. This module will define functions for working with database models.

  • Line 2–9: We create a Django model called Employee which inherits from models.Model . Within this class, we will specify multiple attributes associated with an Employee, including name, emp_id, dob, email, passwrd (password), and occupation. Each attribute is characterized by specific features such as its maximum length, data type, and whether it permits null or blank values.

  • Line 11–12: We defined an __str__ method for the Employee class that returns the employee’s name as a string representation. This is useful for debugging and displaying relevant information.

Conclusion

In Django, null=True and blank=True serve distinct purposes—null=True governs database-level validation by allowing NULL values, while blank=True affects form-level validation, permitting empty fields in user forms. Understanding the difference helps ensure proper handling of data validation in both the database and forms.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the difference between NULL and blank?

null=True controls database-level validation, allowing NULL values. blank=True controls form validation, permitting the field to be left empty in forms.


Why is NULL true?

null=True allows the database to store NULL values for the field, meaning the field can be left empty at the database level.


How to make a field not NULL in Django?

To make a field not NULL in Django, set null=False in the model field definition. This ensures the field cannot have NULL values in the database.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved