What is difference between null=True, blank=True in django.

Oct. 27, 2020
I wonder when we add a database field in django we generally add attributes null=True and blank=True. The same is done with ForeignKey, DecimalField etc. What is the basic difference in those?

1. null=True only
2. blank=True only
3. null=True, blank=True

in respect to different (CharField, ForeignKey, ManyToManyField, DateTimeField) fields. What is difference between using 1/2/3?

1.) null=True
Sets NULL on the column in your DB. This means that your column filed can be Null, objects like DateTime or ForignKey can be null..

2.) Blank = True
This is used with form, It means that in your form this field is not required filed. It can be blank. But if you are saving in DB then you need to mention null=True also or else your DB will give error, but your form will submit.

3.) null=True, blank=True
If you set null=True but Blank is not True, then you need to enter value in form. You can not enter Null value in form. If you want this field to be null in form and DB then you need to configure both as True.

Please Login to Comment