当我们在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管理中保存任何东西时,会发生两步验证,在Django级别和数据库级别。我们不能在数字字段中保存文本。

数据库的数据类型是NULL,什么都不是。当Django在数据库中创建列时,它指定它们不能为空。如果你试图保存NULL,你会得到数据库错误。

同样在Django- admin级别,默认情况下所有字段都是必须的,你不能保存空白字段,Django会给你一个错误。

所以,如果你想保存空白字段,你需要在Django和数据库级别上允许它。 blank=True -在管理面板中允许空字段 null=True -将允许将null保存到数据库列。

其他回答

|| blank = True || null = True && blank = True

class TestModel(models.Model):
    field1 = models.CharField(max_length=100, null=True)
    field2 = models.CharField(max_length=100, blank=True)   # it's not a correct way
    field3 = models.CharField(max_length=100, null=True, blank=True)

数据库字段:MySQL

CREATE TABLE TestModel (
     `id`        INT(10)        NOT     NULL      AUTO_INCREMENT,

     `field1`    VARCHAR(100)   NULL    DEFAULT   NULL,
     `field2`    VARCHAR(100)   NOT     NULL,
     `field3`    VARCHAR(100)   NULL    DEFAULT   NULL,
)

case-01: null = True

db:   db   field is accepts null value
form: form field is `required`

NB: DB IS ACCEPTS NULL VALUE, BUT FORM FIELD IS REQUIRED. SO FORM IS 
SUBMITTED WHEN THIS FIELD HAVE SOME VALUE. it's good.

case-02: blank = True

db:   db   field is not accepts null value
form: form field is `optional`

NB: FORM IS VALID WITHOUT ANY VALUE, BUT DB IS NOT ACCEPTS NULL VALUE.
SO THE FORM IS SUBMITTED WITHOUT ANY VALUE THEN BOOM. it's worst.

case-03: null = True && blank = True

db:   db   field is accepts null value
form: form field is `optional`

NB: HERE FORM FIELD IS OPTIONAL & FORM IS VALID WITHOUT ANY VALUE 
& DB ALSO ACCEPTS NULL VALUE. SO, IT'S BEST TO USE `null=True && blank=True`

:)

null - default为False 如果为True, Django将在数据库中存储为null。

blank - default为False 如果为真,则允许该字段为空

更多的钱,转到 https://docs.djangoproject.com/en/3.0/topics/db/models/

默认值为“null”和“blank”为“False”。

Null:与数据库相关。定义给定的数据库列是否接受空值。

Blank:这是验证相关的。它将在表单验证期间调用form.is_valid()时使用。

也就是说,有一个null=True和blank=False的字段是完全没问题的。意思是在数据库级别上,该字段可以为NULL,但在应用程序级别上,它是必需的字段。

现在,大多数开发人员都犯了错误:为基于字符串的字段(如CharField和TextField)定义null=True。避免这样做。否则,你最终会有两个可能的“无数据”值,即:None和一个空字符串。对于“无数据”有两个可能的值是多余的。Django的约定是使用空字符串,而不是NULL。

正如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的主要区别:

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