I am having a MultipleChoiceField
to select language choices from a list of 7 languages.
LANGUAGES = (
('en', _('English')),
('pl', _('Polish')),
('da', _('Danish')),
)
Inside my forms.py, I have
language = forms.MultipleChoiceField(choices=LANGUAGES, widget=forms.CheckboxSelectMultiple)
I am trying to pre select choices when the page is loaded. I have tried
self.fields['language'].widget.attrs.update({'initial': selected_languages})
and
self.fields['language'].initial = selected_languages
inside __init__
my selected_languages
has value like ['en', 'fr' ]
Edit: The initial added is getting added as an html attribute inside every language checkbox.
<label for="id_language_0"><input type="checkbox" name="language" value="en" initial="['en', 'fr']" id="id_language_0">English</label>
I this the right way to pre select fields in django forms? This method is not working for me. Is there any other method?
NB: I am using this form inside django admin