Python Django 模型字段默认为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4604814/
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
Django Model Field Default to Null
提问by SPoage
I need to have my Django application allow me to have a default value of NULL set for a certain model field. I've looked over the null, blank, and defaultparameters, but it's not very clear what combination of the three I need to use to get the desired effect. I've tried setting default=NULLbut it threw an error. If I specify blank=True, null=Trueand no default, will it just default back to NULL come runtime?
我需要让我的 Django 应用程序允许我为某个模型字段设置默认值 NULL。我查看了null、blank和default参数,但不太清楚我需要使用三者的哪种组合来获得所需的效果。我试过设置,default=NULL但它抛出了一个错误。如果我指定blank=True, null=True并且没有默认值,它会在运行时默认返回 NULL 吗?
采纳答案by gruszczy
Try default=None. There is no NULLin python.
试试default=None。NULL在python 中没有。
回答by Kevin
If you specify null=True on the model field then the value will be stored as NULL in the database if the user does not provide a value.
如果您在模型字段上指定 null=True,则如果用户未提供值,则该值将在数据库中存储为 NULL。
回答by Henning Lee
blank=Trueallows you to input nothing (i.e"",None) and keep it empty.null=Truemeans the database row is allowed to beNULL.default=Nonesets the field toNoneif no other value is given.
blank=True允许您不输入任何内容(即"",None)并将其保留为空。null=True表示允许数据库行为NULL.default=NoneNone如果没有给出其他值,则 将该字段设置为。

