Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django multi-select widget?

The Django admin site makes use of a really cool widget:

enter image description here

How can I make use of this widget in my own applications? I don't see anything like that listed here.

like image 814
mpen Avatar asked Nov 09 '09 00:11

mpen


1 Answers

From the docs:

The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. These widgets define media requirements, and the Django Admin uses the custom widgets in place of the Django defaults. The Admin templates will only include those media files that are required to render the widgets on any given page.

If you like the widgets that the Django Admin application uses, feel free to use them in your own application! They’re all stored in django.contrib.admin.widgets.

In this case, you want the FilteredSelectMultiple widget. To use it, apply the widget on a form field like so:

my_field = forms.ModelMultipleChoiceField(queryset=MyModel.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False)) 

Make sure to include the forms media in the template as it needs to include a few JS files.

like image 181
Bartek Avatar answered Oct 02 '22 14:10

Bartek