{"id":357,"date":"2012-05-26T13:56:19","date_gmt":"2012-05-26T17:56:19","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=357"},"modified":"2012-07-23T11:13:54","modified_gmt":"2012-07-23T15:13:54","slug":"using-the-filterselectmultiple-widget","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2012\/05\/using-the-filterselectmultiple-widget\/","title":{"rendered":"Using the FilterSelectMultiple Widget"},"content":{"rendered":"<p>I will soon be starting a new project at work, and one of the mockups calls for a side by side control to select multiple choices in a many-to-many relationships. \u00c2\u00a0The default control used by Django is the SelectMultiple widget.<\/p>\n<p>&nbsp;<\/p>\n<p>However, this can get unwieldy when the number of choices grows. \u00c2\u00a0Instead, I wish to use a filter select, as seen in the Django admin when assigning permissions to a user or group.<\/p>\n<p>&nbsp;<\/p>\n<p>It didn&#8217;t take too much research to find a Django snippet as an example. \u00c2\u00a0<a href=\"http:\/\/djangosnippets.org\/snippets\/2466\/\">http:\/\/djangosnippets.org\/snippets\/2466\/<\/a>. \u00c2\u00a0I pretty much copied his example, except for the CSS file called &#8216;uid -manage-form.css&#8217;. \u00c2\u00a0This must be part of his application, because I couldn&#8217;t find any other reference to it online.<\/p>\n<p>Here is the form code I ended up with:<\/p>\n<pre>from django.contrib.admin.widgets import FilteredSelectMultiple\r\n\r\nclass BookForm(ModelForm):\r\n    authors = forms.ModelMultipleChoiceField(queryset=Author.objects.all(),\r\n                                          label=('Select Authors'),\r\n                                          widget=FilteredSelectMultiple(\r\n                                                    ('authors'),\r\n                                                    False,\r\n                                                 ))\r\n    class Media:\r\n        css = {\r\n            'all':('\/media\/css\/widgets.css',),\r\n        }\r\n        # jsi18n is required by the widget\r\n        js = ('\/admin\/jsi18n\/',)\r\n\r\n    class Meta:\r\n        model = Book<\/pre>\n<p>As the example shows, I included the tag for the form media in the &lt;head&gt; section of the template, which inserts the CSS and JS calls.<\/p>\n<pre>&lt;head&gt;\r\n    ...\r\n    {{ bookform.media }}\r\n&lt;\/head&gt;<\/pre>\n<p>Once my designer gets a hold of this, I&#8217;m sure it will look great.<\/p>\n<p>UPDATE: For an example of using the\u00c2\u00a0RelatedFieldWidgetWrapper to include the capability to add a new record, see <a title=\"RelatedFieldWidgetWrapper\" href=\"http:\/\/dashdrum.com\/blog\/2012\/07\/relatedfieldwidgetwrapper\/\">this post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will soon be starting a new project at work, and one of the mockups calls for a side by side control to select multiple choices in a many-to-many relationships. \u00c2\u00a0The default control used by Django is the SelectMultiple widget. &nbsp; However, this can get unwieldy when the number of choices grows. \u00c2\u00a0Instead, I wish &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2012\/05\/using-the-filterselectmultiple-widget\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using the FilterSelectMultiple Widget&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-357","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=357"}],"version-history":[{"count":8,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":375,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/357\/revisions\/375"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}