Beginner: Creating a form with variable number of checkboxes

204 views
Skip to first unread message

T Kwn

unread,
Dec 2, 2014, 7:27:47 PM12/2/14
to django...@googlegroups.com
I'm creating a page where the user can add other users to a group. I am able to dynamically fetch all users and list them on a template with checkboxes. I'm getting stuck creating a form for them however.

I read this post (among others)

http://stackoverflow.com/questions/19947538/django-form-with-unknown-number-of-checkbox-fields-and-multiple-actions

I don't understand what should be used to define "choices" in this example. He says fill it with a queryset but which one?

choices = queryset_of_valid_choices,

Would that be something like objects_list.all()?


Currently my form is defined as this:

class UpdateGroup(forms.Form):
non_member_checkboxes = forms.MultipleChoiceField(
widget = forms.CheckboxSelectMultiple,
)

I know it's missing something because the form.is_valid is always False.

Collin Anderson

unread,
Dec 4, 2014, 8:30:07 AM12/4/14
to django...@googlegroups.com
Hi,

The forms.ModelMultipleChoiceField might help you out here.

class UpdateGroup(forms.Form):
    non_member_checkboxes
= forms.ModelMultipleChoiceField(
        queryset
=User.objects.all(),
        widget
=forms.CheckboxSelectMultiple,
   
)

Collin
Reply all
Reply to author
Forward
0 new messages