14

I have a non-admin form in which I'd like to use filter_horizontal on. I have read this which does much more than what I want (I only want the filter_horizontal). I wanted to check to see if anyone has come up with a simpler (more current) way to just implement the filter_horizontal.

So here is the code:

class County(models.Model):
    """County Names"""
    name = models.CharField(max_length=64)
    state = USStateField(null=True)

class Company(models.Model):
    """The basics of a company"""
    name = models.CharField(max_length = 100)
    counties = models.ManyToManyField(County,blank=True, null=True)

Then our form currently look like this. I thought this would work..

from django.contrib.admin.widgets import FilteredSelectMultiple
class RaterCompanyForm(ModelForm):
    class Meta:
        model = RaterOrganization
        exclude = ('remrate_projects',)
        widgets = {'counties': FilteredSelectMultiple(verbose_name="Counties",
                                                      is_stacked=True,) }
    class Media:
        css = {'all':['admin/css/widgets.css']}
        js = ['/admin/jsi18n/']

BTW: I understand this may be a duplicate of this but his question wasn't answered. I've done plenty of homework here and here but neither of these appear to work.

2 Answers 2

16

I know this thread is old, but hopefully this info will help someone else who stumbles upon this page like I did.

After much pain and suffering, I was able to get this to work with Django 1.4. Like rh0dium, I tried all those articles, but had to make a lot of tweaks.

You don't have to do anything special with the ModelForm, but you do have to include all these js and css files in the template:

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/core.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/jquery.init.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectFilter2.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}admin/js/SelectBox.js"></script>

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/widgets.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/base.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}admin/css/forms.css"/>

Then render the form as you normally would, but you need to get the fieldset elements and class names right for the css to work. For example:

<fieldset>
    <div class="form-row">
        <form method="post" action=".">
            {% csrf_token %}
            {{ form.as_p }}
        <button type="submit" value="submit">Add</button>
    </form>
  </div>
</fieldset>

Then at the BOTTOM of the template (after the markup to render the form), add this script and replace pricetags with whatever your Many to Many (M2M) relationship name is on the model form's model:

<script type="text/javascript">
    addEvent(window, "load", function(e) { SelectFilter.init("id_pricetags", "pricetags", 0, "{{ STATIC_URL }}admin/"); });
</script>

Apparently your media location may be something different, but {{ STATIC_URL }}admin/ worked for me.

5
  • You can change the 0 to a 1 in the SelectFilter.init call to make it vertical instead of horizontal. Apr 1, 2013 at 21:07
  • use {% admin_media_prefix %} instead of {{ STATIC_URL }}
    – est
    Jul 10, 2014 at 3:03
  • Only this worked for me <script type="text/javascript" src="{% static "admin/js/core.js" %}"></script> Aug 11, 2015 at 17:30
  • Have a look @ stackoverflow.com/questions/45858181/…
    – openHBP
    Jan 21, 2021 at 16:47
  • still relevant in 2021........ Oct 16, 2021 at 18:41
4

I got this working very easily in Django 3 in 2020; perhaps things have changed since the question was asked in 2011. All I needed to do was set the widget of the form field, no custom Media defined on the class (django will add it automatically based on the widgets used):

class FooForm(forms.Form):
    linked_bars = forms.ModelMultipleChoiceField(queryset=Bar.objects.all(),
                         widget=widgets.FilteredSelectMultiple(Bar._meta.verbose_name_plural, False))
# end of class, no Media!

You DO need to have the jsi18n loaded globally so in a base template I have:

{% block extrahead %}
  {{ block.super }}
  <script type="text/javascript" src="/admin/jsi18n/"></script>
{% endblock %}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.