在djang中,differentication null=True,blank=True

2024-04-26 01:35:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在django中添加数据库字段时,我们通常会编写:

models.CharField(max_length=100, null=True, blank=True)

ForeignKeyDecimalField等也一样。拥有

  1. ^仅{}
  2. ^仅{}
  3. null=Trueblank=True

对于不同的(CharFieldForeignKeyManyToManyFieldDateTimeField)字段。使用1/2/3有什么优点/缺点?


Tags: django数据库truemodelsnulllengthmax优点
3条回答

null=True在数据库中的列上设置NULL(而不是NOT NULL)。Django字段类型(如DateTimeFieldForeignKey)的空值将作为NULL存储在数据库中。

blank=True确定表单中是否需要该字段。这包括管理员和您自己的自定义表单。如果blank=True,则不需要该字段,而如果是False,则该字段不能为空。

这两者的组合非常频繁,因为通常如果要允许表单中的某个字段为空,则还需要数据库允许该字段的NULL值。例外情况是CharFields和TextFields,在Django中,它们是never另存为NULL。空白值作为空字符串存储在数据库中('')。

举几个例子:

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

Django从不将CHARTEXT类型保存为NULL,因此null=True是不必要的。但是,您可以手动将其中一个字段设置为None,以强制将其设置为NULL。如果您有一个可能需要这样做的场景,那么您仍然应该包含null=True

这就是ORM如何映射Django 1.8的blank&;null字段

class Test(models.Model):
    charNull        = models.CharField(max_length=10, null=True)
    charBlank       = models.CharField(max_length=10, blank=True)
    charNullBlank   = models.CharField(max_length=10, null=True, blank=True)

    intNull         = models.IntegerField(null=True)
    intBlank        = models.IntegerField(blank=True)
    intNullBlank    = models.IntegerField(null=True, blank=True)

    dateNull        = models.DateTimeField(null=True)
    dateBlank       = models.DateTimeField(blank=True)
    dateNullBlank   = models.DateTimeField(null=True, blank=True)        

PostgreSQL 9.4创建的数据库字段包括:

CREATE TABLE Test (
  id              serial                    NOT NULL,

  "charNull"      character varying(10),
  "charBlank"     character varying(10)     NOT NULL,
  "charNullBlank" character varying(10),

  "intNull"       integer,
  "intBlank"      integer                   NOT NULL,
  "intNullBlank"  integer,

  "dateNull"      timestamp with time zone,
  "dateBlank"     timestamp with time zone  NOT NULL,
  "dateNullBlank" timestamp with time zone,
  CONSTRAINT Test_pkey PRIMARY KEY (id)
)

为MySQL 5.6创建的数据库字段是:

CREATE TABLE Test (
     `id`            INT(11)     NOT  NULL    AUTO_INCREMENT,

     `charNull`      VARCHAR(10) NULL DEFAULT NULL,
     `charBlank`     VARCHAR(10) NOT  NULL,
     `charNullBlank` VARCHAR(10) NULL DEFAULT NULL,

     `intNull`       INT(11)     NULL DEFAULT NULL,
     `intBlank`      INT(11)     NOT  NULL,
     `intNullBlank`  INT(11)     NULL DEFAULT NULL,

     `dateNull`      DATETIME    NULL DEFAULT NULL,
     `dateBlank`     DATETIME    NOT  NULL,
     `dateNullBlank` DATETIME    NULL DEFAULT NULL
)

如Django模型字段引用中所述: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

如果True,则该字段允许为空。默认为False

注意,这与null不同。null是纯粹的 与数据库相关,而blank与验证相关。如果一个字段 具有blank=True,表单验证将允许输入空值。 如果字段有blank=False,则该字段是必需的。

相关问题 更多 >