nelsonvarela
nelsonvarela

Reputation: 2370

django admin list_filter too long

I have a list_filter with loads of sectors. This list, on the right side of the page, is too long.

Can I use an input select field instead since I can't choose more than one sector?

I have seen this before, screenshots, but I can not find the way to do this.

edit:

I have a custom FilterSpec not a list_filter

Upvotes: 3

Views: 2035

Answers (4)

nelsonvarela
nelsonvarela

Reputation: 2370

This is how i resolved it (jQuery):

 $('#changelist-filter ul').each(function(){

        var maxlength = 10;

        if ($(this).children().length > maxlength )
        {
            var list=$(this),
                select=$(document.createElement('select')).insertBefore($(this).hide());

            $('>li a', this).each(function(){
                console.log($(this).parent().attr('class'));

                var target=$(this).attr('target'),
                    option=$(document.createElement('option'))
                        .appendTo(select)
                        .val(this.href)
                        .attr('selected', $(this).parent().attr('class'))
                        .html($(this).html())
                        .click(function(){
                            if (target==='_blank'){
                                window.open($(this).val());
                            }
                            else{
                                window.location.href=$(this).val();
                            }
                        });
            });
            list.remove();

        }
    });

Upvotes: 0

okm
okm

Reputation: 23871

The long list that you said comes from the default template 'admin/filter.html', in django/contrib/admin/templates/admin/filter.html, of builtin ListFilters.

There are several ways to customize it:

  1. Globally override 'admin/filter.html'. Render select tag instead of ul tag if the count of choices hit certain limit. This affects all list filters in admin. The select tag should have onchange event handler like

    <select ... onchange="location.href=this.options[this.selectedIndex].value">

  2. Set template attribute in your specific ListFilter instance, to the name of the customized filter template. The content of the template is like #1. Django 1.4+ is required for this.

  3. Add javascript in the ModelAdmin instance to convert HTML content inside the ul tag to select tag as soon as DOM has been fully loaded.

Upvotes: 0

Mariusz Jamro
Mariusz Jamro

Reputation: 31633

You can write your own custom FilterSpec (custom admin list filter).

This feature is not part of the Django code yet; it is planned for version 1.2. You'll need to apply this patch to the Django code: http://code.djangoproject.com/ticket/5833.

There are many examples on stackoverflow on how to do that, e.g: https://stackoverflow.com/a/1294952/342473.

Upvotes: 1

Related Questions