bbrooke
bbrooke

Reputation: 2347

Set initial value of checkbox dynamically

I have a MultipleChoiceField with a CheckboxSelectMutliple widget:

weight_training_days = forms.MultipleChoiceField(
    help_text=u'(Required) 3 days must be selected',
    widget=forms.CheckboxSelectMultiple(attrs={
        'inline': True,
    }),
    choices=(
        (0, "Mon"),
        (1, "Tue"),
        (2, "Wed"),
        (3, "Thu"),
        (4, "Fri"),
        (5, "Sat"),
        (6, "Sun"),
    ),
)

What I'm trying to is dynamically set 3 of the 7 checkboxes to "True". Ideally I would do this from the view.

def change_challenge_settings_page(request):

    c = Challenge.objects.get(user__exact = request.user,chal_status=1)

    layout = 'horizontal'
    form =UpdateChallengeSettingsForm(initial={'goal': c.level_goal }) 

    return render(request, 'portal/portal_change_challenge_settings.html', {'form': form,'layout': layout,'scorecard_page': True,})

I know how to do this with ChoiceFields (in the example above the "goal" is a ChoiceField) but am stuck when it comes to MultipleChoiceFields. I really appreciate any thoughts/feedback.

Upvotes: 4

Views: 6239

Answers (3)

amchugh89
amchugh89

Reputation: 1296

OP says they ideally want to do this in the view, and I find that often it is the case that you want these specified dynamically at view-level and not form-level.

This is how you do that at view level

    form = MySelectForm()
    form.fields['my_choice_field'].initial = [1,2,3] 

You can also dynamically specify the choices (not just the initially-selected choices) with something like:

    form.fields['my_choice_field'].choices = [1,2,3,4,5,6,7] 

Upvotes: 0

Wolph
Wolph

Reputation: 80061

It's fairly simple, just pass a list. Here's a full example:

from django import forms
class UpdateChallengeSettingsForm(forms.Form):
    weight_training_days = forms.MultipleChoiceField(
        help_text=u'(Required) 3 days must be selected',
        widget=forms.CheckboxSelectMultiple(attrs={
            'inline': True,
        }),
        choices=(
            (0, "Mon"),
            (1, "Tue"),
            (2, "Wed"),
            (3, "Thu"),
            (4, "Fri"),
            (5, "Sat"),
            (6, "Sun"),
        ),
    )

form = UpdateChallengeSettingsForm(initial={
    'weight_training_days': [1,2,4,5,6],
})

print form.as_p()

And the output:

<p><label for="id_weight_training_days_0">Weight training days:</label> <ul>
<li><label for="id_weight_training_days_0"><input inline="True" type="checkbox" name="weight_training_days" value="0" id="id_weight_training_days_0" /> Mon</label></li>
<li><label for="id_weight_training_days_1"><input checked="checked" name="weight_training_days" value="1" inline="True" type="checkbox" id="id_weight_training_days_1" /> Tue</label></li>
<li><label for="id_weight_training_days_2"><input checked="checked" name="weight_training_days" value="2" inline="True" type="checkbox" id="id_weight_training_days_2" /> Wed</label></li>
<li><label for="id_weight_training_days_3"><input inline="True" type="checkbox" name="weight_training_days" value="3" id="id_weight_training_days_3" /> Thu</label></li>
<li><label for="id_weight_training_days_4"><input checked="checked" name="weight_training_days" value="4" inline="True" type="checkbox" id="id_weight_training_days_4" /> Fri</label></li>
<li><label for="id_weight_training_days_5"><input checked="checked" name="weight_training_days" value="5" inline="True" type="checkbox" id="id_weight_training_days_5" /> Sat</label></li>
<li><label for="id_weight_training_days_6"><input checked="checked" name="weight_training_days" value="6" inline="True" type="checkbox" id="id_weight_training_days_6" /> Sun</label></li>
</ul> <span class="helptext">(Required) 3 days must be selected</span></p>

Upvotes: 1

Ngenator
Ngenator

Reputation: 11269

You can set the initial to be a list. In this case if you set YourForm(inital={'weight_training_days': [0,1,2]}) it will default to having Monday, Tuesday and Wednesday selected. You can also do it like so forms.MultipleChoiceField(... inital=[0,1,2] ...)

Upvotes: 5

Related Questions