当我们在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的优点/缺点是什么?


当前回答

这里,是null=True和blank=True的主要区别:

null和blank的默认值都是False。这两个值都在字段级工作,即,我们是否想要保持字段为空或空白。

null=True将字段的值设置为null,即没有数据。它基本上是为数据库列的值。

date = models.DateTimeField(null=True)

blank=True确定表单中是否需要该字段。这包括管理表单和您自己的自定义表单。

title = models.CharField(blank=True) // title可以为空。 在数据库中(“”)将被存储。 null=True blank=True这意味着该字段在所有情况下都是可选的。

epic = models.ForeignKey(null=True, blank=True)
// The exception is CharFields() and TextFields(), which in Django are never saved as NULL. Blank values a

其他回答

在模型中null=True和blank=True的含义还取决于这些字段在表单类中的定义方式。

假设你已经定义了以下类:

class Client (models.Model):
    name = models.CharField (max_length=100, blank=True)
    address = models.CharField (max_length=100, blank=False)

如果表单类是这样定义的:

class ClientForm (ModelForm):
    class Meta:
        model = Client
        fields = ['name', 'address']
        widgets = {
            'name': forms.TextInput (attrs = {'class': 'form-control form-control-sm'}),
            'address': forms.TextInput (attrs = {'class': 'form-control form-control-sm'})
        }

然后,'name'字段将不是强制性的(由于模型中的空白=True), 'address'字段将是强制性的(由于模型中的空白=False)。

然而,如果ClientForm类是这样定义的:

class ClientForm (ModelForm):
    class Meta:
        model = Client
        fields = ['name', 'address']

    name = forms.CharField (
        widget = forms.TextInput (attrs = {'class': 'form-control form-control-sm'}),
    )
    address = forms.CharField (
        widget = forms.TextInput (attrs = {'class': 'form-control form-control-sm'}),
    )

然后,这两个字段('name'和'address')将是强制性的,“因为声明式定义的字段保持原样”(https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/),即表单字段的'required'属性的默认值为True,这将要求字段'name'和'address'被填充,即使在模型中,字段已被设置为blank=True。

正如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表单中没有验证约束。所以当你为这个模型填写一个modelForm时,你可以不填这个选项。

Null纯粹与数据库相关,而blank与验证相关。如果一个字段为blank=True, Django管理站点的验证将允许输入一个空值。如果一个字段有blank=False,该字段将是必需的

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()以编程方式设置任何缺失的值。