在django中区分null = True,空白= True

问题:在django中区分null = True,空白= True

当我们在Django中添加数据库字段时,通常会这样写:

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

同样是与做ForeignKeyDecimalField等有什么根本区别在其

  1. null=True 只要
  2. blank=True 只要
  3. null=Trueblank=True

在相对于不同的(CharFieldForeignKeyManyToManyFieldDateTimeField)字段。使用1/2/3有哪些优点/缺点?

When we add a database field in django we generally write:

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

The same is done with ForeignKey, DecimalField etc. What is the basic difference in having

  1. null=True only
  2. blank=True only
  3. null=True, blank=True

in respect to different (CharField, ForeignKey, ManyToManyField, DateTimeField) fields. What are the advantages/disadvantages of using 1/2/3?


回答 0

null=True在数据库的列中设置NULL(与相对NOT NULL)。Django字段类型(例如DateTimeField或)的空白值ForeignKey将存储NULL在数据库中。

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

两者的组合是如此频繁,因为通常如果您要允许表单中的字段为空白,则还需要数据库来允许NULL该字段的值。CharFields和TextFields 是一个exceptions,在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

CHARTEXT类型永远不会NULL被Django 保存,因此null=True没有必要。但是,您可以手动设置这些字段之一None以强制将其设置为NULL。如果您有可能需要这样做的情况,则仍应包括null=True

null=True sets NULL (versus NOT NULL) on the column in your DB. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the DB.

blank determines whether the field will be required in forms. This includes the admin and your custom forms. If blank=True then the field will not be required, whereas if it’s False the field cannot be blank.

The combo of the two is so frequent because typically if you’re going to allow a field to be blank in your form, you’re going to also need your database to allow NULL values for that field. The exception is CharFields and TextFields, which in Django are never saved as NULL. Blank values are stored in the DB as an empty string ('').

A few examples:

models.DateTimeField(blank=True) # raises IntegrityError if blank

models.DateTimeField(null=True) # NULL allowed, but must be filled out in a form

Obviously, Those two options don’t make logical sense to use (though there might be a use case for null=True, blank=False if you want a field to always be required in forms, optional when dealing with an object through something like the 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 and TEXT types are never saved as NULL by Django, so null=True is unnecessary. However, you can manually set one of these fields to None to force set it as NULL. If you have a scenario where that might be necessary, you should still include null=True.


回答 1

这是Django 1.8 的ORM映射blanknull字段的方式

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
)

This is how the ORM maps blank & null fields for 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)        

The database fields created for PostgreSQL 9.4 are :

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)
)

The database fields created for MySQL 5.6 are :

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
)

回答 2

如Django模型字段参考中所述:链接

栏位选项

以下参数可用于所有字段类型。所有都是可选的。


null

Field.null

如果为True,则Django将NULL在数据库中存储空值。默认值为False

避免null在基于字符串的字段(例如CharField和)上使用, TextField因为空字符串值将始终存储为空字符串,而不是NULL。如果基于字符串的字段具有null=True,则表示它具有两个“无数据”的可能值:NULL和空字符串。在大多数情况下,为“无数据”设置两个可能的值是多余的。Django约定是使用空字符串,而不是 NULL

对于基于字符串的字段和基于非字符串的字段,blank=True如果您希望允许表单中的空值,还需要进行设置,因为该null参数仅影响数据库存储(请参阅参考资料blank)。

注意

使用Oracle数据库后端时,无论此属性如何,都将存储值NULL表示空字符串


blank

Field.blank

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

请注意,这与有所不同nullnull与数据库完全相关,而blank与验证相关。如果字段包含blank=True,则表单验证将允许输入一个空值。如果字段包含blank=False,则将需要该字段。

As said in 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.


回答 3

理解Django模型字段定义中的选项至少有两个作用是至关重要的:定义数据库表,定义默认格式和验证模型形式。(我之所以说“默认值”,是因为可以始终通过提供自定义表单来覆盖这些值。)某些选项影响数据库,某些选项影响表单,而某些选项同时影响这两种形式。

关于nullblank,其他答案已经明确表明,前者影响数据库表定义,而后者影响模型验证。我认为,通过查看所有四种可能配置的用例,可以使区分更加清楚:

  • null=Falseblank=False:这是默认的配置和手段,该值在所有情况下需要。

  • null=Trueblank=True:表示该字段在所有情况下都是可选的。(但是,如下所述,这不是使基于字符串的字段为可选的推荐方法。)

  • null=Falseblank=True:表示表单不需要值,但是数据库需要。有许多用例:

    • 最常见的用法是用于基于字符串的可选字段。如文档中所述,Django习惯用法是使用空字符串表示缺少的值。如果NULL还允许,您将最终以两种不同的方式指示缺失值。

    • 另一种常见情况是,您想根据另一个字段的值自动计算一个字段(例如,使用您的save()方法)。您不希望用户以某种形式提供值(因此blank=True),但是您希望数据库强制始终提供值(null=False)。

    • 另一个用途是当您想要指示a ManyToManyField是可选的时。因为此字段是作为单独的表而不是数据库列实现的,null所以没有意义blank不过,的值仍会影响表单,控制在没有关系时验证是否成功。

  • null=Trueblank=False:表示表单需要一个值,但数据库不需要。这可能是最不常用的配置,但是有一些用例:

    • 要求用户始终包含一个值是完全合理的,即使您的业务逻辑实际上并不需要它也是如此。毕竟,表单只是添加和编辑数据的一种方式。您可能拥有的代码生成的数据不需要与人工编辑器一样严格的验证。

    • 我看到的另一个用例是当你有一个ForeignKey你不想允许级联删除的情况。也就是说,在正常使用中,该关系应始终存在(blank=False),但是如果它指向的对象恰好被删除,则您也不想删除该对象。在这种情况下,您可以使用null=Trueon_delete=models.SET_NULL实现一种简单的软删除

It’s crucial to understand that the options in a Django model field definition serve (at least) two purposes: defining the database tables, and defining the default format and validation of model forms. (I say “default” because the values can always be overridden by providing a custom form.) Some options affect the database, some options affect forms, and some affect both.

When it comes to null and blank, other answers have already made clear that the former affects the database table definition and the latter affects model validation. I think the distinction can be made even clearer by looking at use cases for all four possible configurations:

  • 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.

    • 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 which doesn’t need the same stringent validation that 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.


回答 4

您可能有答案,但是直到今天,仍然很难判断是否将null = True或blank = True或两者都放在一个字段中。我个人认为为开发人员提供这么多的选择是非常无用的,而且令人困惑。让句柄根据需要为空或空格。

我遵循这张表,来自Django的两个独家新闻

You may have your answer however till this day it’s difficult to judge whether to put null=True or blank=True or both to a field. I personally think it’s pretty useless and confusing to provide so many options to developers. Let the handle the nulls or blanks however they want.

I follow this table, from Two Scoops of Django:


回答 5

简单null=True定义数据库应该接受NULL值,另一方面,blank=True在表单验证中定义此字段是否应该接受空白值(如果blank=True它接受该字段中没有值的表单,并且blank=False在表单验证中为[默认值],它将显示此字段为必填错误。

null=True/False 与数据库有关

blank=True/False 与表单验证有关

Simply null=True defines database should accept NULL values, on other hand blank=True defines on form validation this field should accept blank values or not(If blank=True it accept form without a value in that field and blank=False[default value] on form validation it will show This field is required error.

null=True/False related to database

blank=True/False related to form validation


回答 6

这是带有blank= True和的字段的示例null=True

description = models.TextField(blank = True,null = True)

在这种情况下:: blank = True告诉我们的表格可以将描述字段留空

null = True:告诉我们的数据库可以在db字段中记录一个空值并且不给出错误。

Here is an example of the field with blank= True and null=True

description = models.TextField(blank=True, null= True)

In this case: blank = True: tells our form that it is ok to leave the description field blank

and

null = True: tells our database that it is ok to record a null value in our db field and not give an error.


回答 7

这里,是的主要区别null=Trueblank=True

两者的默认值nullblank值为False。这两个值都在字段级别起作用,即我们是否要保留字段nullblank

null=True将字段的值设置为NULL即无数据。它基本上是针对数据库列的值。

date = models.DateTimeField(null=True)

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

title = models.CharField(blank=True) // title can be kept blank. 在数据库("")中将被存储。 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

Here, is the main difference of null=True and blank=True:

The default value of both null and blank is False. Both of these values work at field level i.e., whether we want to keep a field null or blank.

null=True will set the field’s value to NULL i.e., no data. It is basically for the databases column value.

date = models.DateTimeField(null=True)

blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms.

title = models.CharField(blank=True) // title can be kept blank. In the database ("") will be stored. null=True blank=True This means that the field is optional in all circumstances.

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

回答 8

null = True

意味着对于要填充的字段没有数据库的约束,因此您可以拥有一个具有空值的对象,该对象具有此选项。

blank = True

意味着没有django形式的验证约束。因此,当您modelForm为此模型填写时,可以不填写此选项。

null = True

Means there is no constraint of database for the field to be filled, so you can have an object with null value for the filled that has this option.

blank = True

Means there is no constraint of validation in django forms. so when you fill a modelForm for this model you can leave field with this option unfilled.


回答 9

null和blank的默认值为False。

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

空白:与验证相关。调用form.is_valid()时,将在表单验证期间使用它。

话虽这么说,具有null = True和blank = False的字段是完全可以的。在数据库级别上,该字段可以为NULL,但是在应用程序级别上,它是必填字段。

现在,大多数开发人员都将其弄错了:为基于字符串的字段(如CharField和TextField)定义null = True。避免这样做。否则,您最终将获得两个可能的“无数据”值,即:和空字符串。为“无数据”设置两个可能的值是多余的。Django约定是使用空字符串,而不是NULL。

The default values of null and blank are False.

Null: It is database-related. Defines if a given database column will accept null values or not.

Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid().

That being said, it is perfectly fine to have a field with null=True and blank=False. Meaning on the database level the field can be NULL, but in the application level it is a required field.

Now, where most developers get it wrong: Defining null=True for string-based fields such as CharField and TextField. Avoid doing that. Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL.


回答 10

当我们在Django admin中保存任何内容时,将在Django级别和数据库级别进行两步验证。我们无法在数字字段中保存文本。

数据库的数据类型为NULL,没什么。当Django在数据库中创建列时,它指定它们不能为空。而且,如果您尝试保存NULL,则会出现数据库错误。

同样在Django-Admin级别,默认情况下所有字段都是必填字段,您无法保存空白字段,Django会抛出错误。

因此,如果要保存空白字段,则需要在Django和数据库级别允许它。blank = True-将允许管理面板中的空字段null = True-将允许将NULL保存到数据库列。

When we save anything in Django admin two steps validation happens, on Django level and on Database level. We can’t save text in a number field.

Database has data type NULL, it’s nothing. When Django creates columns in the database it specifies that they can’t be empty. And if you will try to save NULL you will get the database error.

Also on Django-Admin level, all fields are required by default, you can’t save blank field, Django will throw you an error.

So, if you want to save blank field you need to allow it on Django and Database level. blank=True – will allow empty field in admin panel null=True – will allow saving NULL to the database column.


回答 11

有一点null=True甚至在CharField或上也有必要TextField,那就是数据库unique为列设置了标志。

换句话说,如果您在Django中具有唯一的Char / TextField,则需要使用以下代码:

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

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

There’s one point where null=True would be necessary even on a CharField or TextField and that is when the database has the unique flag set for the column.

In other words, if you’ve a unique Char/TextField in Django, you’ll need to use this:

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

For non-unique CharField or TextField, you’ll be better off skipping the null=True otherwise some fields will get set as NULL while others as “” , and you’ll have to check the field value for NULL everytime.


回答 12

是数据库和空白是字段的验证要显示在文本框一样的用户界面得到的人的姓氏。如果lastname = models.charfield(blank = true),则没有要求用户输入姓氏,因为这是可选字段。如果lastname = models.charfield(null = true), 则意味着如果该字段未从用户那里获取任何值,则它将作为空字符串“”存储在数据库中。

null is for database and blank is for fields validation that you want to show on user interface like textfield to get the last name of person. If lastname=models.charfield (blank=true) it didnot ask user to enter last name as this is the optional field now. If lastname=models.charfield (null=true) then it means that if this field doesnot get any value from user then it will store in database as an empty string ” “.


回答 13

模型中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'})
        }

然后,“名称”字段将不是强制性的(由于模型中的blank = True),而“地址”字段将是强制性的(由于模型中的blank = 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'}),
    )

然后,两个字段(“名称”和“地址”)都是必填字段“因为以声明方式定义的字段保持原样”https://docs.djangoproject.com/zh/3.0/topics/forms/modelforms/) ,即表单字段的’required’属性的默认值为True,即使在模型中将该字段设置为blank = True,这也将要求填写字段’name’和’address’。

The meaning of null=True and blank=True in the model also depends on how these fields were defined in the form class.

Suppose you have defined the following class:

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

If the form class has been defined like this:

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'})
        }

Then, the ‘name’ field will not be mandatory (due to the blank=True in the model) and the ‘address’ field will be mandatory (due to the blank=False in the model).

However, if the ClientForm class has been defined like this:

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'}),
    )

Then, both fields (‘name’ and ‘address’) will be mandatory, “since fields defined declaratively are left as-is” (https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/), i.e. the default for the ‘required’ attribute of the form field is True and this will require that the fields ‘name’ and ‘address’ are filled, even if, in the model, the field has been set to blank=True.


回答 14

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

空白-如果为true,则默认值为False,该字段允许为空白

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

null – default is False if True, Django will store empty as null in the database.

blank – default is False if true that field is allowed to be blank

more, goto https://docs.djangoproject.com/en/3.0/topics/db/models/


回答 15

下表说明了主要区别:

+--------------------------------------------------------------------+
| Purpose                  | null=True        | blank = True         |
|--------------------------|------------------|----------------------|
| Field can be empty in DB | Do this          | Unaffected           |
|--------------------------|------------------|----------------------|
| ModelForm(required field)| Unaffected       | field not required   |
|--------------------------|------------------|----------------------|
| Form Validation          | Unaffected       | field not required   |
|--------------------------|------------------|----------------------|
| on_delete=SET_NULL       | Need this        | Unaffected           |
+--------------------------------------------------------------------+

This table below demonstrates the main differences:

+--------------------------------------------------------------------+
| Purpose                  | null=True        | blank = True         |
|--------------------------|------------------|----------------------|
| Field can be empty in DB | Do this          | Unaffected           |
|--------------------------|------------------|----------------------|
| ModelForm(required field)| Unaffected       | field not required   |
|--------------------------|------------------|----------------------|
| Form Validation          | Unaffected       | field not required   |
|--------------------------|------------------|----------------------|
| on_delete=SET_NULL       | Need this        | Unaffected           |
+--------------------------------------------------------------------+

回答 16

在很简单的话

空白不同于空值。

纯粹数据库相关的,而空白是验证相关的(在形式所需)

如果是null=True,Django会的store empty values as NULL in the database。如果有一个字段blank=True,则将进行表单验证allow entry of an empty value。如果字段的空白为False,则将需要该字段。

In Very simple words,

Blank is different than null.

null is purely database-related, whereas blank is validation-related(required in form).

If null=True, Django will store empty values as NULL in the database. 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.