null=True
controls database-level validation, allowing NULL values. blank=True
controls form validation, permitting the field to be left empty in forms.
Key takeaways:
null=True
allows a field in the database to storeNULL
values, enabling the database column to remain empty.
blank=True
permits form fields to be left empty during validation, making them optional for users to fill out.Both
null=True
andblank=True
are used to control the behavior of fields at different levels: database vs. form validation.An exception exists for
CharField
andTextField
, which handle empty values by storingNone
or an empty string instead ofNULL
.
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 occupationname = 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.
null
argumentThe 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.
blank
argumentThe blank
argument is used to specify whether or not a field is allowed to be left empty in 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.
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)
?
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)
#.../emp_id = models.CharField(max_length=100, blank=True)#.../
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 thenull=True
statement is removed from the code.
Bud1�godbdscldjangodbdsclboolwebsitedsclbool @� @� @� @E�DSDB `� @� @� @
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.
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.
Haven’t found what you were looking for? Contact Us
Free Resources