当我们在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的优点/缺点是什么?


当前回答

这里有一个blank= True和null=True字段的示例

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

在这种情况下: blank = True:告诉我们的表单可以将description字段保留为空

and

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

其他回答

理解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”和“blank”为“False”。

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

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

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

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

简单的回答是:Null是数据库表,Blank是Django表单。

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

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

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

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

根据文档,null真正与数据库相关。如果null=true, DB将null输入存储为null。否则,空字符串将被存储为空字符串。 然而,如果blank=true, form将验证它为ok,否则该字段将被form视为“必需”。

默认为false。