gpt4 book ai didi

django - django的model field options `` blank `` and ` `null `` when they' re not set的默认值是多少?

转载 作者:行者123 更新时间:2023-12-03 21:28:32 25 4
gpt4 key购买 nike

documentation ,我无法找到答案。

如果我想让一个字段留空,我是否必须明确设置 blank=True ?

编辑:我瞎了。或者愚蠢。谢谢你向我指出这一点;)

最佳答案

我知道你已经链接了文档,但我明确地向遇到相同问题的任何人提出这一点。不过,您的标题问题的答案在文档中明确而明确-两者都是 False默认情况下。如果您希望该字段为 blank=True ,那么是的,您需要在每个字段上手动指定。

空白用于表格:

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.



如果您在该字段中保存没有任何内容的记录,则 Null 是存储在数据库中的内容。同样,此字段的属性是 null=False

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



在很多情况下,您希望表单为空白,但数据库条目为 not Null - 例如, you were specifying your own custom slug on model save .

当然,这也意味着任何自定义 Field s 你来自主 Field类将具有相同的默认行为。如果这真的困扰你,你可以覆盖 Field用你想要的行为来上课,但是为你之后的其他人调试真的很麻烦。

关于django - django的model field options `` blank `` and ` `null `` when they' re not set的默认值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42470083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com