Django multi-select widget?

DjangoFormsDjango Forms

Django Problem Overview


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.

Django Solutions


Solution 1 - Django

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - DjangoBartekView Answer on Stackoverflow