Chetan Chetan - 1 year ago 204
Javascript Question

Django admin many to many horizontal filter not working

I'm using a many-to-many field in my django model, exposing it on the admin interface and trying to use a filter_horizontal widget. Now, I've added some custom html to my change_form.html so simply adding a

filter_horizontal = ('teams', 'players', )
is not helping.

I googled around, and found out that the widget requires some default js, so I went ahead and included the following in my change_form.html

<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin/js/vendor/jquery/jquery.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/SelectBox.js"></script>
<script type="text/javascript" src="/static/admin/js/SelectFilter2.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>

I see the following error in my browser's console right now:

Uncaught ReferenceError: interpolate is not definedinit @
SelectFilter2.js:47 (anonymous function) @ (index):1275

Digging into the SelectFilter2.js, I found that it requires a file addevent.js. I'm guessing this is where the interpolate function would've been defined since all other imports are satisfied. But I cannot find any such file in my project, and google did not help out on this. Can anyone help me here? What am I missing?

Answer Source

So for whatever it's worth, the horizontal filter didn't work, and this did the job for me easily so I went ahead with this:

raw_id_fields = ('teams', 'players', )

Also, please don't forget to include the following in your custom template for this widget to work:

<script type="text/javascript" src="/static/admin/js/admin/RelatedObjectLookups.js"></script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download