In the documentation, I was not able to find an answer to this.
If I want a field to be left blank, do I have to explicitly set blank=True
?
EDIT: I'm blind. Or stupid. Thanks for pointing that out to me ;)
The fundamental difference between these two is that null controls the validation at the database level, and blank is used during form field validation at the application level. Consider the following model with a title field, a date field, and a time field: from django.
default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. null: If True , Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string).
null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.
Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField. Similarly, for other fields.
I know you've linked the docs, but I'm putting this out explicitly for anyone that comes across the same question. The answer to your headline question is explicitly and clearly there in the docs, though - both are False
by default. If you want the field to be blank=True
, then yes, you'll need to manually specify on every field.
Blank is for Forms:
If True, the field is allowed to be blank. Default is False.
Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.
Null is what is stored the Database if you save a record with nothing in that field. Again, the field attribute for this is null=False
If True, Django will store empty values as NULL in the database. Default is False.
Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.
For both string-based and non-string-based fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).
There are plenty of occasions when you'd want the form to be blank, but the database entry to be not Null
- if for example, you were specifying your own custom slug on model save.
This also means, of course, that any custom Field
s you derive from the main Field
class will have the same default behaviour. If that really bothers you, you could override the Field class with your desired behaviour, but that would be really janky to debug for the others who come after you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With