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
精彩评论