开发者

How can I have a default value in a non required django form field?

class MyForm(forms.Form):
   ...
   my_field = forms.Char开发者_JS百科Field(required=False)

By default, my_field will have None, if it wasn't completed. How can I make it have a default value? Is there any way I wouldn't have to use initial? I don't want it displaying the default value inside the widget.


When you pull the value from the form, set it to the default if it has no value.

my_value = myform.cleaned_data['my_field'] or SOME_DEFAULT_VALUE
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜