当我们在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模型字段定义中的选项(至少)有两个目的是至关重要的:定义数据库表,定义模型表单的默认格式和验证。(我说“默认”是因为可以通过提供自定义表单来覆盖这些值。)一些选项会影响数据库,一些选项会影响表单,还有一些会同时影响两者。

说到null和blank,其他答案已经说明,前者影响数据库表定义,后者影响模型验证。我认为,通过查看所有四种可能配置的用例,可以更清楚地区分它们:

null=False, blank=False: This is the default configuration and means that the value is required in all circumstances. null=True, blank=True: This means that the field is optional in all circumstances. As noted below, though, this is not the recommended way to make string-based fields optional. null=False, blank=True: This means that the form doesn't require a value but the database does. There are a number of use cases for this: The most common use is for optional string-based fields. As noted in the documentation, the Django idiom is to use the empty string to indicate a missing value. If NULL was also allowed you would end up with two different ways to indicate a missing value. (If the field is also unique, though, you'll have to use null=True to prevent multiple empty strings from failing the uniqueness check.) Another common situation is that you want to calculate one field automatically based on the value of another (in your save() method, say). You don't want the user to provide the value in a form (hence blank=True), but you do want the database to enforce that a value is always provided (null=False). Another use is when you want to indicate that a ManyToManyField is optional. Because this field is implemented as a separate table rather than a database column, null is meaningless. The value of blank will still affect forms, though, controlling whether or not validation will succeed when there are no relations. null=True, blank=False: This means that the form requires a value but the database doesn't. This may be the most infrequently used configuration, but there are some use cases for it: It's perfectly reasonable to require your users to always include a value even if it's not actually required by your business logic. After all, forms are only one way of adding and editing data. You may have code that is generating data that doesn't need the same stringent validation you want to require of a human editor. Another use case that I've seen is when you have a ForeignKey for which you don't wish to allow cascade deletion. That is, in normal use the relation should always be there (blank=False), but if the thing it points to happens to be deleted, you don't want this object to be deleted too. In that case you can use null=True and on_delete=models.SET_NULL to implement a simple kind of soft deletion.

其他回答

Null是数据库和空白是字段验证,你想显示在用户界面上,如textfield,以获得人的姓。 如果lastname =模型。Charfield (blank=true)它没有要求用户输入姓氏,因为这是可选字段现在。 如果lastname =模型。Charfield (null=true),那么这意味着如果这个字段没有从user得到任何值,那么它将存储在数据库作为一个空字符串“”。

这就是ORM在Django 1.8中映射空白和空字段的方法

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
)

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。

如果设置null=True,它将允许将数据库列的值设置为null。如果你只设置blank=True, django会为这个列设置默认的新值为""。

有一点,null=True将是必要的,即使在CharField或TextField,这是当数据库有唯一的标志设置列。在这种情况下,你需要使用这个:

a_unique_string = models.CharField(blank=True, null=True, unique=True)

最好跳过null=True的非唯一CharField或TextField。否则,一些字段将被设置为NULL,而另一些字段将被设置为“”,并且您必须每次检查字段值是否为NULL。

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是数据库级别的约束。