How to use CheckboxSelectMultiple() widget

95 views
Skip to first unread message

mojito

unread,
Feb 4, 2010, 1:27:19 AM2/4/10
to Django users
I have a list of files I'd like to render as a list of check boxes
with multiple selection. Then from the frontend I want to add the
ability to save and delete these files. How can I achieve this using
CheckboxSelectMultiple() widget ?
Thanks

Bill Freeman

unread,
Feb 4, 2010, 12:38:13 PM2/4/10
to django...@googlegroups.com
I'll take you at your word that you have a list of file (names?), rather than
a queryset.

class xxx(forms.Form):
def __init__(self, list_of_files, *args, **kwargs):
self.fields['files'].choices = enumerate(list_of_files)
super(xxx, self).__init__(*args, **kwargs)

files = forms.MultipleChoiceField(widget=CheckboxSelectMultiple, choices=[])

You then put multiple submit buttons on the form so that you can choose to save
the checked files, delete the checked files, or what (you need to give
them a "name"
attribute to make them show up in the POST data). When your view instantiates
the form, you pass the list of files. When a posted form if valid,
form.checked_data['files']
will be a list of indexes into list of files. You need to be sure
that you have the same
list when you get the post as you had for the get that rendered the form.

Untested, but I've done similar. Should be close.

Bill

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages