当我们在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.
null=True和blank=True是django.db.models中的字段属性。Null是与数据库相关的,而空白是与验证相关的。
null
默认值为null=False。如果null=False, Django将不允许在数据库列中使用null值。
如果null=True, Django会将数据库列中的空值存储为null。对于CharField和TextField, django将使用空字符串"而不是NULL。避免为CharField和TextField使用空属性。一个例外是,当CharField具有unique=True和blank=True时,则需要null=True。
空白
默认为空白=False。如果blank=False,该字段将是必需的。
如果blank=True,该字段是可选的,可以留空。blank=True和null=False将需要在模型上实现clean()以编程方式设置任何缺失的值。
blank=True可以设置为任何模型字段,以控制在表单中输入值时该字段是否可以留空。这里,我们讨论的是输入数据。
null=True, if we set blank=True for a field, that model field does not receive any value, then the database or Django has to do something with that field when data is written into the database. For any kind of text content an empty string is stored in the database, so there is a value stored in the database. For other kinds of fields like date fields or numbers, we use the special data type "null". "null" can be used if a field potentially has no value, but by default, Django does not allow "null" values. That is why you need to explicitly set null=True.
假设你为任何非文本字段设置了blank=True,但你没有指定“null=True”,Django将不知道存储什么,它会抛出一个错误。
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(而不是NOT null)。Django字段类型(如DateTimeField或ForeignKey)的空白值将在DB中存储为NULL。
Blank确定表单中是否需要该字段。这包括管理表单和自定义表单。如果blank=True,则该字段将不是必需的,而如果为False,则该字段不能为空。
这两者的组合非常常见,因为通常情况下,如果您要允许表单中的字段为空,您还需要数据库允许该字段为NULL值。例外是CharFields和TextFields,它们在Django中永远不会被保存为NULL。空白值作为空字符串存储在DB中(")。
举几个例子:
models.DateTimeField(blank=True) # raises IntegrityError if blank
models.DateTimeField(null=True) # NULL allowed, but must be filled out in a form
显然,使用这两个选项在逻辑上没有意义(尽管如果您希望一个字段在表单中始终是必需的,则可能存在null=True, blank=False的用例,当通过shell之类的东西处理对象时是可选的)。
models.CharField(blank=True) # No problem, blank is stored as ''
models.CharField(null=True) # NULL allowed, but will never be set as NULL
CHAR和TEXT类型永远不会被Django保存为NULL,所以NULL =True是不必要的。但是,您可以手动将其中一个字段设置为None以强制将其设置为NULL。如果你有一个场景,在那里可能是必要的,你仍然应该包括null=True。