Python 在 Django models.py 中,default、null 和 blank 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4384098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Django models.py, what's the difference between default, null, and blank?
提问by TIMEX
null=Trueblank=Truedefault = 0
null=Trueblank=Truedefault = 0
What's the difference? When do you use what?
有什么不同?你什么时候用什么?
采纳答案by Chris Morgan
Direct from Django model field reference:
直接来自Django 模型字段参考:
Field.nullIf
True, Django will store empty values asNULLin the database. Default isFalse.Note that empty string values will always get stored as empty strings, not as
NULL. Only usenull=Truefor non-string fields such as integers, booleans and dates. For both types of fields, you will also need to setblank=Trueif you wish to permit empty values in forms, as thenullparameter only affects database storage (seeblank).Avoid using
nullon string-based fields such asCharFieldandTextFieldunless you have an excellent reason. If a string-based field hasnull=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;” Django convention is to use the empty string, notNULL.
Field.null如果
True,Django 将像NULL在数据库中一样存储空值。默认为False。请注意,空字符串值将始终存储为空字符串,而不是
NULL. 仅null=True用于非字符串字段,例如整数、布尔值和日期。对于这两种类型的字段,您还需要设置blank=True是否希望在表单中允许空值,因为该null参数仅影响数据库存储(请参阅 参考资料blank)。避免
null在基于字符串的字段上使用CharField,TextField除非您有充分的理由。如果基于字符串的字段具有null=True,则意味着它有两个可能的“无数据”值:NULL 和空字符串。在大多数情况下,“无数据”有两个可能的值是多余的;Django 约定是使用空字符串,而不是NULL.
Field.blankIf
True, the field is allowed to be blank. Default isFalse.Note that this is different than
null.nullis purely database-related, whereasblankis validation-related. If a field hasblank=True, validation on Django's admin site will allow entry of an empty value. If a field hasblank=False, the field will be required.
Field.blank如果
True,则允许该字段为空。默认为False。请注意,这与
null.null纯粹与数据库相关,而blank与验证相关。如果字段具有blank=True,则 Django 管理站点上的验证将允许输入空值。如果字段具有blank=False,则该字段将是必需的。
Field.defaultThe default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
Field.default字段的默认值。这可以是值或可调用对象。如果可调用,则每次创建新对象时都会调用它。
回答by Ben Regenspan
From docs:
从文档:
nullIf True, Django will store empty values as NULL in the database. Default is False.
blankIf True, the field is allowed to be blank. Default is False.
defaultThe default value for the field.
null如果为 True,Django 将在数据库中将空值存储为 NULL。默认值为假。
blank如果为 True,则该字段可以为空。默认值为假。
default字段的默认值。
You can use "default" to set the value that will be used for the field in question should your code not explicitly set it to a value.
如果default您的代码未明确将其设置为某个值,您可以使用“ ”来设置将用于相关字段的值。
Use "blank" for form validation purposes - blank=True will allow the field to be set to an empty value
使用“ blank”进行表单验证 - blank=True 将允许将字段设置为空值
Use "null" if you would like to store an empty value as "null" in the DB. Often it's preferred, however, to set blank values to an empty string or to 0 as appropriate for a given field.
null如果您想在数据库中将空值存储为“null”,请使用“ ”。但是,通常首选将空白值设置为空字符串或根据给定字段的需要设置为 0。
回答by aliasav
In implementation terms:
在实施方面:
The 'blank' field corresponds to all forms. Specifies if this value is required in form and corresponding form validation is done. 'True' allows empty values.
“空白”字段对应于所有表格。指定表单中是否需要此值并完成相应的表单验证。'True' 允许空值。
The 'null' field corresponds to DB level. It will be set as NULL or NOT NULL at the DB.
'null' 字段对应于 DB 级别。它将在数据库中设置为 NULL 或 NOT NULL。
Hence if leave a field empty in admin with blank=true, NULL is fed into the DB. Now this might throw an error if that particular column in the DB is specified as NOT NULL.
因此,如果将 admin 中的字段留空,且空白 = true,则将 NULL 输入到数据库中。现在,如果数据库中的特定列被指定为 NOT NULL,这可能会引发错误。
回答by saran3h
I know you already have your answer however till this day it's difficult to judge whether to put null=Trueor blank=Trueor bothto 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.
我知道你已经有答案然而直到今天它来判断是否把很难null=True或blank=True或both到现场。我个人认为为开发人员提供如此多的选择是非常无用和令人困惑的。让他们随意处理空值或空白。
回答by sharif_42
Null: It is database-related. Defines if a given database column will accept null values or not.
Null:它与数据库相关。定义给定的数据库列是否接受空值。
Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid().
空白:与验证相关。它将在表单验证期间使用,当调用 form.is_valid() 时。
Default: All Time it store the given value(default value) to the field if one doesn't provide any value for this field.
默认值:如果没有为此字段提供任何值,它将始终将给定值(默认值)存储到该字段。
The default values of null and blank are False.
null 和 blank 的默认值为False。
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.
话虽如此,拥有一个 null=True 和 blank=False 的字段是完全没问题的。这意味着在数据库级别该字段可以为 NULL,但在应用程序级别它是必填字段。
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: Noneand an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL.
现在,大多数开发人员都弄错了:为基于字符串的字段(例如 CharField 和 TextField)定义 null=True。避免这样做。否则,您最终将有两个可能的“无数据”值,即:无和空字符串。“无数据”有两个可能的值是多余的。Django 约定是使用空字符串,而不是 NULL。


