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

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

null=True Vs blank=True in Django

Gaurav Sharma's photo
Gaurav Sharma
·May 18, 2022·

2 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Introduction:

These two arguments in a Django model field are used widely, but one is connected with Django and the other is connected to Django forms.

null=True :

Well null=True represents that at database level for any column in a database table, we can store NULL as a value.

blank=True :

blank=True is more related to Django forms than database, in a Django form, at the data validation stage, Django runs the required field validation only on the basis of blank=True field.

So if blank is set to False, which means no empty values (i.e. "") can be passed in the form.

If blank is set to True which means a user can skip a form field, but the value will be stored as NULL in the database.

Now, the thing to keep in mind is that if you are using blank=True, then you have to make sure null=True on the same field. Because Django, no matter what, is going to store the value as NULL at the database level.

Conclusion:

So null=True & blank=True is a quite frequently used combination in a Django field. But in a nutshell, null=True is more of like a database level validation, while blank=True is a Django forms level validation.

For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.

Did you find this article valuable?

Support Dev.Junction by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this