Key takeaways:
null=Trueallows a field in the database to storeNULLvalues, enabling the database column to remain empty.
blank=Truepermits form fields to be left empty during validation, making them optional for users to fill out.Both
null=Trueandblank=Trueare used to control the behavior of fields at different levels: database vs. form validation.An exception exists for
CharFieldandTextField, which handle empty values by storingNoneor 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
dobfield. Observe the behavior of the code both before and after thenull=Truestatement is removed from the code.