admin filters as select boxes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# put this in a templatetag library:
from django.template import Library

register = Library()



def admin_list_filter_select(cl, spec):
    choices = list(spec.choices(cl))
    # query_string of a non-all option will contain the name of our param (skipping '?'):
    # ?param1=x&fieldname_param=y
    key = (k.split('=')[0] for k in choices[-1]['query_string'][1:].split('&')
           if k.startswith(spec.field.name)).next()
    for c in choices:
        # find the option's value by looking after our key and skipping '='
        if key in c['query_string']:
            c['val'] = c['query_string'].split(key)[1].split('&')[-1][1:]
        else:
            c['val'] = ''    
    return {'field_name': key,
            'title': spec.title(), 
            'choices' : choices}
            
admin_list_filter_select = register.inclusion_tag('admin/filter_select.html')(admin_list_filter_select)



# template: 
{% load i18n %}
<label>{% blocktrans with title|escape as filter_title %} By {{ filter_title }} {% endblocktrans %}</label>
<select name="{{ field_name }}">
{% for choice in choices %}
    <option {% if choice.selected %} selected="selected"{% endif %} value="{{ choice.val }}">{{ choice.display|escape }}</option>
{% endfor %}
</select>



# modify your admin/change_list.hml to include the templatetag lib and this code: (for example
# in the search block:
...
{% block search %}{% search_form cl %}
{% if cl.has_filters %}
<form action="." method="get">
{% for spec in cl.filter_specs %}
   {% admin_list_filter_select cl spec %}
{% endfor %}
<input type="submit"/>
</form>
{% endif %}
{% endblock %}

More like this

  1. Transparently encrypt ORM fields using OpenSSL (via M2Crypto) by ncoghlan 1 year, 10 months ago
  2. Querystring Builder - create urls with GET params by jibberia 2 years, 3 months ago
  3. Null Field Admin Filter by bcurtu 4 years, 4 months ago
  4. Yet another query string template tag by atms 2 years, 1 month ago
  5. Allow foreign key attributes in list_display with '__' by jcushman 3 months, 2 weeks ago

Comments

softmechanics (on January 6, 2010):

I found a somewhat simpler method: create a new template admin/filter.html containing:

{% load i18n %}            
<h3>{% blocktrans with title as filter_title %} By {{ filter_title }} {% endblocktrans %}</h3>
<div align="right">
  <select onChange="javascript:window.location = this.options[this.selectedIndex].value;" style="width: 80%">
    {% for choice in choices %}
      <option {% if choice.selected %}selected{% endif %} value="{{ choice.query_string|iriencode }}">
        {{ choice.display }}
      </option>
    {% endfor %}           
  </select>                
</div>

#

gamesbook (on October 19, 2010):

The above code is great - just one small change:

Use - style="width:90%; margin-right:5%;"

to keep the drop down box neatly centred in the filter div.

#

(Forgotten your password?)