[Django]-Django – Preselect options in MultipleChoiceField

4👍

You only need to set initial values to your field like this:

class YourForm(forms.Form):
    choices = (
          ('choice1':'choice1'),
          ('choice2':'choice2'),
          ('choice3':'choice3')
           )
   #for example if you want to select choice1 and choice2
   initial_values = ['choice1', 'choice2']
   field = forms.MultipleChoiceField(initial=initial_values)

1👍

Use Django ModelForm to do this. When instantiating, provide inital data to the form.

For example:

def your_view(request):
    form = YourModelForm(initial={'choice_field_name': list of choices})

Leave a comment