90👍
Direct from Django model field reference:
Field.null
If
True
, Django will store empty values asNULL
in the database. Default isFalse
.Note that empty string values will always get stored as empty strings, not as
NULL
. Only usenull=True
for non-string fields such as integers, booleans and dates. For both types of fields, you will also need to setblank=True
if you wish to permit empty values in forms, as thenull
parameter only affects database storage (seeblank
).Avoid using
null
on string-based fields such asCharField
andTextField
unless you have an excellent reason. If a string-based field hasnull=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;” Django convention is to use the empty string, notNULL
.
Field.blank
If
True
, the field is allowed to be blank. Default isFalse
.Note that this is different than
null
.null
is purely database-related, whereasblank
is validation-related. If a field hasblank=True
, validation on Django’s admin site will allow entry of an empty value. If a field hasblank=False
, the field will be required.
Field.default
The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
22👍
From docs:
null
If True, Django will store empty
values as NULL in the database.
Default is False.
blank
If True, the field is allowed to
be blank. Default is False.
default
The default value for the
field.
You can use “default
” to set the value that will be used for the field in question should your code not explicitly set it to a value.
Use “blank
” for form validation purposes – blank=True will allow the field to be set to an empty value
Use “null
” if you would like to store an empty value as “null” in the DB. Often it’s preferred, however, to set blank values to an empty string or to 0 as appropriate for a given field.
- [Django]-Django – how to create a file and save it to a model's FileField?
- [Django]-Can't install via pip because of egg_info error
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
21👍
I know you already have your answer however till this day it’s difficult to judge whether to put null=True
or blank=True
or both
to a field. I personally think it’s pretty useless and confusing to provide so many options to developers. Let the handle the nulls or blanks however they want.
- [Django]-Django access the length of a list within a template
- [Django]-Pylint "unresolved import" error in Visual Studio Code
- [Django]-How about having a SingletonModel in Django?
3👍
Null: It is database-related. Defines if a given database column will accept null values or not.
Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid().
Default: All Time it store the given value(default value) to the field if one doesn’t provide any value for this field.
The default values of null and blank are False.
That being said, it is perfectly fine to have a field with null=True and blank=False. Meaning on the database level the field can be NULL, but in the application level it is a required field.
Now, where most developers get it wrong: Defining null=True for string-based fields such as CharField and TextField. Avoid doing that. Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL.
- [Django]-How to use subquery in django?
- [Django]-How to debug Django commands in PyCharm
- [Django]-How do you dynamically hide form fields in Django?
2👍
In implementation terms:
The ‘blank’ field corresponds to all forms. Specifies if this value is required in form and corresponding form validation is done.
‘True’ allows empty values.
The ‘null’ field corresponds to DB level. It will be set as NULL or NOT NULL at the DB.
Hence if leave a field empty in admin with blank=true, NULL is fed into the DB. Now this might throw an error if that particular column in the DB is specified as NOT NULL.
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."
- [Django]-How to create a fixture file
- [Django]-Django Model – Get distinct value list