Categories
Python Answers

How to set the selected value on a Python Django forms.ChoiceField

Spread the love

Sometimes, we want to set the selected value on a Python Django forms.ChoiceField.

In this article, we’ll look at how to set the selected value on a Python Django forms.ChoiceField.

How to set the selected value on a Python Django forms.ChoiceField

To set the selected value on a Python Django forms.ChoiceField, we set the initial argument when we’re calling the form constructor.

For instance, we write

form = MyForm(initial={'max_number': '3'})

to create the MyForm instance with the initial argument set to {'max_number': '3'} to set the initial value of the max_number field to 3.

Conclusion

To set the selected value on a Python Django forms.ChoiceField, we set the initial argument when we’re calling the form constructor.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *