Syntax
field_name = models.Field (blank = True)
Django inline field validation blank = True
Explanation
Illustration of null = True using the example. Consider a project named pythonengineering
that has an application named geeks
.
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into the models.py
file of the geeks application. We’ll use CharField to experiment with all the margin variations.
|
After running makemigrations and migrating to Django and rendering the above model, let’s try creating an instance using None from the Django shell. To start the Django shell, enter the command,
Python manage.py shell
Now let’s try to create a GeeksModel instance using a empty string .
|
Let’s check in the admin interface to see if the model has been instantiated.
Therefore, blank = True changes the field to be empty values or Python terms " Values".
Extended Concepts with Empty = True
Note that this is different from Zero . null is purely database related, whereas blank is related to validation. If the field is blank = True
, the form validation will allow a blank value to be entered. If the field is blank = False
, the field will be required. null = True
and blank = True
are often used at the same time. The combination of the two is so common because generally, if you want a field to be empty on your form, you also need your database to allow NULL values for that field.