The django framework has two popular field options, blank and null, which django developers should know the difference between the two.
Although each is an optional setting for any given model field, both default to False. So the question is, when should you set either – or both – to True?
Following is a guide to understanding the difference of blank vs null fields, and when it may be appropriate to setting them to True.
The blank option actually doesn’t have to do with the database. It has to do with validation of your web forms. When blank is set to True it tells your form to allow the form to return with that field being empty. In other words, blank=true tells django that this field is NOT required for your form to get submitted.
Another way of thinking about the blank option is to understand that since blank is set to False by default, this means that any field added to a model is going to be considered required for any web form in which django validates this field. So you want to set blank=true when you don’t want this field to be required when validating a form.
The null option does concern your database. Setting null=True is the option that tells the database to allow empty (null) values to be stored for that field. Again, the default for this field is False, therefore, django will not allow null values for any field unless you set null equal to true.
When to use blank and null
In most cases you will probably want to set both options the same way. But this isn’t always required. If you have blank=true for a field, it will just store it as an empty string in the database, so null=true won’t be necessary.
The question of when to have different settings is going to depend on your applications individual needs.
The excellent book, Two Scoops of Django: Best Practices For Django actually has a full guide on when to use either option, in the chapter on database/model best practices.
I recommend picking up this book for this and much more on web development with django!