1π
β
Is simpler than that. When you instantiate the form, pass it with the argument data
a dictionary with the keyword visitors
as a list with the identifiers of your choices field that you want to be selected (the left most value in the choices tuples).
For example, assume you have this:
class SelectedVisitorsForm(forms.Form):
CHOICE = (
(1,'user'),
(2, 'admin'),
(3, 'guest'),
)
visitors = forms.MultipleChoiceField(required=True, choices=CHOICES)
Then when you instantiate this form in the views
, do it like this:
form = SelectedVisitorsForm(data={visitors:[1,3]})
And that will show the MultipleChoiceField with user
and guest
selected.
I hope it helps!
π€Paulo Bu
Source:stackexchange.com