开发者

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

开发者 https://www.devze.com 2023-03-09 13:09 出处:网络
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 a
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

精彩评论

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