views:

1006

answers:

1
+5  A: 

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.

Bartek
Hrm... doesn't want to work for some reason. All the JS is there, but it isn't converting the selectbox for some reason. http://7src.com/~mnb2/a3/access
Mark
Can you paste some code to http://dpaste.com?
Bartek
Yes: http://dpaste.com/hold/118024/
Mark
This is a wild guess but try setting your second argument (for the `is_stacked` variable) to False. Looking at the code, it looks like that may affect the display of two boxes or not. Unfortunately I can't test it myself right now so I'm just looking through code hehe :)
Bartek
Nevermind. Found the problem. It needs `<script type="text/javascript" src="/~mnb2/a3/admin/jsi18n/"></script>` as well, which isn't included by `form.media`.
Mark
Interesting that it needs that, good stuff though!
Bartek
Oh, I should also mention that you need to be logged in as a superuser just to access that file!! I recommend saving the file out and including that instead.
Mark