Filter_horizontal, applying filter on the right box - chosen items

435 views
Skip to first unread message

Aeh. ABID

unread,
Jan 5, 2015, 12:39:39 AM1/5/15
to django...@googlegroups.com
I'm wondering if there is a standard way to add a javascript-based filter on the right box like the one in the  left
when having manytomany relation with filter_horizontal.

Collin Anderson

unread,
Jan 8, 2015, 12:50:22 PM1/8/15
to django...@googlegroups.com
Hi,

It's a bit hacky, but I've used filter_horizontal outside of the admin before.

from django.contrib.admin import widgets

class MyForm(forms.Form):
    users
= forms.ModelMultipleChoiceField(
        required
=False,
        widget
=widgets.FilteredSelectMultiple(verbose_name='Field Name', is_stacked=False),
        queryset
=choices_qs,
        initial
=initial_qs,
   
)

<link rel="stylesheet" href="/static/admin/css/widgets.css">
<script>function addEvent(obj, evType, fn){obj.addEventListener(evType, fn, false)}</script>
<div>{{ form }}</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>django = {jQuery: $}</script>
<script src="/admin/jsi18n/"></script>
{{ form.media }}

Collin

ebuild

unread,
Jan 8, 2015, 2:04:29 PM1/8/15
to django...@googlegroups.com
This doesn't answer the question.

Collin Anderson

unread,
Jan 9, 2015, 9:00:50 AM1/9/15
to django...@googlegroups.com
Hi,

I mis-read. Sorry. You're looking for a text field above the right box so you can more easily search for items in that box. I have not seen that done before. If nothing else you could try setting it up as an Inline and raw_id_fields.

Collin

Dustin Dobernig

unread,
Apr 6, 2016, 1:57:54 PM4/6/16
to Django users
I know this is question is old but I was looking for the solution myself and couldn't find an quick answer so I ended up just doing it myself. My solution is here https://djangosnippets.org/snippets/10560/

dario.f...@sgdigital.com

unread,
Apr 25, 2018, 10:54:10 AM4/25/18
to Django users
Hi all, 
Are you aware of a standard solution that has been added to django in the meanwhile?

Or adding Dustin's solution to my code is the way to go?

Cheers :)
Reply all
Reply to author
Forward
0 new messages