Add special field lookups to the Admin list_filter display

 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
$(document).ready(function() {
    function customFilter(heading, filter, text) {
        var filtergroup = $('#changelist-filter h3:contains("'+ heading +'")').next('ul');
        qs = window.location.search;

        // if we're already filtering by a sibling (that isn't "All")
        if (filtergroup.children('li:first').siblings().is('.selected')) {
            // remove sibling querystring from custom filter querystring
            var filterBase = filter.substr(0, filter.indexOf('_'));
            var siblingQs = qs.substr(qs.indexOf(filterBase), qs.indexOf('&', qs.indexOf(filterBase)));
            if (!siblingQs) {
                siblingQs = qs.slice(qs.indexOf(filterBase), qs.length);
            }
            qs = qs.replace(siblingQs, '');
            qs = qs + '&' + filter;
        } else {
            // no current filters, make new
            if (!qs || qs == '?') {
                qs = '?' + filter;
            // already filtering by custom filter, ignore
            } else if (qs.match(filter)) {
                qs = qs;
            // current filters, append
            } else {
                qs = qs + '&' + filter;
            }
        }

        filtergroup.append('<li><a href="'+ qs +'">'+ text +'</a></li>');

        var currentfilter = filtergroup.children('li:last');
        if (window.location.search.match(filter)) {
            currentfilter.addClass('selected');
            currentfilter.siblings().each(function(){
                $(this).removeClass('selected');
                $(this).children('a').attr('href', function() {
                    return this.href.replace(filter, '');
                });
            });
        }
    }

    // Usage examples. Takes three arguments.
    // 1. the exact text above your list_display section
    // 2. the custom field lookup (careful with weird characters here)
    // 3. the text you want the new filter to show up as
    customFilter('By affiliation', 'school__in=A,B', 'School A or B');
    customFilter('By affiliation', 'school__isnull=True', 'No school');
});

More like this

  1. Unobtrusvie Foldable Admin Interface by whiteinge 6 years, 2 months ago
  2. Add extra form elements in your contib admin by sergejdergatsjev 5 years, 4 months ago
  3. admin: edit related object shortcut by christian 6 years, 6 months ago
  4. A dict template tag by Batiste 6 years ago
  5. Confirm alert if the user navigates away without saving changes by mrazzari 4 years, 9 months ago

Comments

Dinnerdog (on January 13, 2010):

In your example you have set 'By affiliation' as the heading. How do you get this to appear above yout list_display section?

For example I want to use 'By LEA' but when I set this as the heading nothing appears in the filters. However if I set the heading to an already existing heading then my options appear.

Thanks

#

(Forgotten your password?)