jeudi 31 mai 2018

How to pre select django forms.CheckboxSelectMultiple

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' ]

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




Aucun commentaire:

Enregistrer un commentaire