当我们在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.
Django模型中的每个选项都有两个目的
在数据库级别定义字段约束(例如SQL, Postgresql,或任何其他)
在表单级别定义字段约束(在数据库层之上的框架级别)
现在让我们回到零和空白
空白是Django表单相关的。它用于在admin或Django中验证Django表单。特别是当我们调用form.is_valid()时
Null与数据库相关。它告诉底层数据库该列是否允许保存空值。
例如,让我们看看下面的例子-
class Company(models.Model):
name = models.CharField(max_length=100)
website = models.UrlField()
founded_on = models.DateField(blank=True, null=False)
random_date = models.DateFeild(blank=False, null=True)
random_text = models.TextField(null=True, blank=True)
我已经定义了一个Company模型,它有两个字段,我们在其中玩空白和空选项。让我们看看不同的字段会发生什么
founded_on: can receive an empty string value at form level (framework/language level). While saving to the database then we would raise IntegrityError because the Database will not accept the null value due to null being false.
random_date: receiving an empty value at form level (Framework) through validation error, since blank is not allowed due to blank true that is setting constraints at the form level. However, it also allows the column to be null at the database layer.
random_text: This is the option that means that the field is allowed to be saved as null at the database layer and also empty string value is allowed to be valid data as per the Django forms validation logic due to blank=True. So in short it can receive empty values (at the framework level and can store empty value at DB level.
要解决所有这些困惑,请将数据库提交视为两层过程。
首先,它填写表单,我们可以在框架级别调用验证数据。
其次,它有一个数据库级别的选项,可以帮助定义DB约束。
这里blank是框架级别的东西,而null是数据库级别的约束。
如果设置null=True,它将允许将数据库列的值设置为null。如果你只设置blank=True, django会为这个列设置默认的新值为""。
有一点,null=True将是必要的,即使在CharField或TextField,这是当数据库有唯一的标志设置列。在这种情况下,你需要使用这个:
a_unique_string = models.CharField(blank=True, null=True, unique=True)
最好跳过null=True的非唯一CharField或TextField。否则,一些字段将被设置为NULL,而另一些字段将被设置为“”,并且您必须每次检查字段值是否为NULL。