0

I have a Django form:

class PlayerForm(forms.Form):
    OPTIONS = MyUser.objects.all()
    players = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                             queryset=OPTIONS)

I want the initial values of the players to be checked if MyUser.player = True. How do I set these initial values?

1 Answer 1

1

You need to set the initial attribute on the players field to be a list of the relevant MyUser ids. A quick way to do this is to use the initial keyword argument of ModelMultipleChoiceField:

players = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple,
                                         queryset=OPTIONS,
                                         initial=[u.pk for u in OPTIONS if u.player])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.