在Django models.py中,default、null和blank有什么区别?
null=True
blank=True
default = 0
有什么区别?什么时候用什么?
null=True
blank=True
default = 0
What's the difference? When do you use what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
直接来自 Django 模型字段参考:
Direct from Django model field reference:
来自文档:
如果您的代码未明确将其设置为某个值,您可以使用“
default
”来设置将用于相关字段的值。使用“
blank
”进行表单验证 - Blank=True 将允许将该字段设置为空值如果您想存储空值,请使用“
null
”在数据库中为“null”。然而,通常首选将空白值设置为空字符串或适合给定字段的 0。From docs:
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.Use "
blank
" for form validation purposes - blank=True will allow the field to be set to an empty valueUse "
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=True
或blank=True
或both
放入场地。我个人认为向开发人员提供如此多的选择是非常无用且令人困惑的。让他们随意处理空值或空白。我遵循这张表(来自《Django 的两勺》一书):
I know you already have your answer however till this day it's difficult to judge whether to put
null=True
orblank=True
orboth
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 the book "Two Scoops of Django"):
Null:与数据库相关。定义给定的数据库列是否接受空值。
空白:与验证相关。它将在表单验证期间调用 form.is_valid() 时使用。
默认:如果没有为此字段提供任何值,则始终将给定值(默认值)存储到该字段。
null 和空白的默认值为 False。
话虽这么说,有一个 null=True 和 Blank=False 的字段是完全可以的。这意味着在数据库级别该字段可以为 NULL,但在应用程序级别它是必填字段。
现在,大多数开发人员都会犯错误:为基于字符串的字段(例如 CharField 和 TextField)定义 null=True。避免这样做。否则,您最终将得到两个可能的“无数据”值,即:无和空字符串。对于“无数据”有两个可能的值是多余的。 Django 约定是使用空字符串,而不是 NULL。
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().
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.
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.
在实现方面:
“空白”字段对应于所有表单。指定表单中是否需要此值并完成相应的表单验证。
“True”允许空值。
“null”字段对应于数据库级别。它将在数据库中设置为 NULL 或 NOT NULL。
因此,如果在 admin 中将一个字段保留为空并且空白=true,则 NULL 将被输入到数据库中。现在,如果数据库中的特定列被指定为 NOT NULL,则可能会引发错误。
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.
The 'null' field corresponds to DB level. It will be set as NULL or NOT NULL at the DB.
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.