当我们在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”和“blank”为“False”。

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

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

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

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

正如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.

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将不知道存储什么,它会抛出一个错误。

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