当我们在Django中添加模型字段时,我们通常这样写:
models.CharField(max_length=100, null=True, blank=True)
ForeignKey, DecimalField等也是如此。两者的基本区别是什么:
null = True只
空白= True只
null=True, blank=True
对于不同的(CharField, ForeignKey, ManyToManyField, DateTimeField)字段?使用选项1、2或3的优点/缺点是什么?
正如Django Model Field reference中所说:Link
Field options
The following arguments are available to all field types. All are optional.
null
Field.null 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).
Note
When using the Oracle database backend, the value NULL will be stored to denote the empty string regardless of this attribute
blank
Field.blank
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.