What is the difference between null=True and blank=True in Django?
null=True Vs blank=True in Django
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.
Social Links
- LinkedIn: https://www.linkedin.com/in/mnamegaurav/
- YouTube: https://www.youtube.com/c/devjunction
- Website: https://gaurav.devjunction.in/
- GitHub: https://github.com/mnamegaurav
- Instagram: https://www.instagram.com/mnamegaurav/
- Twitter: https://twitter.com/mnamegaurav