geex-arts / django-jet

Modern responsive template for the Django admin interface with improved functionality. We are proud to announce completely new Jet. Please check out Live Demo
https://github.com/jet-admin/jet-bridge
GNU Affero General Public License v3.0
3.58k stars 773 forks source link

Admin - multiselect filter for m2m and fk - how to? #40

Open carlosfvieira opened 8 years ago

carlosfvieira commented 8 years ago

Hi, i'm trying to use multiselect filter in modeladmin, but the rendered control is always the normal single selection dropdown. What do i need to change to get the control that i want? Also, django-jet already have this functionality built-in?

Thanks!

list_filter = (('education_level', IntersectionFieldListFilter), 'tutor_category',)

My multiselect list filters are based on this project: extraadminfilters... here's the code. filters.py:

from django.utils.translation import ugettext_lazy as _
from django.contrib.admin.filters import FieldListFilter
from django.db.models.fields import IntegerField, AutoField
import logging

class MultipleSelectFieldListFilter(FieldListFilter):

    def __init__(self, field, request, params, model, model_admin, field_path):
        self.lookup_kwarg = '%s_filter' % field_path
        self.filter_statement = '%s__id' % field_path
        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
        self.lookup_choices = field.get_choices(include_blank=False)
        super(MultipleSelectFieldListFilter, self).__init__(
            field, request, params, model, model_admin, field_path)

    def expected_parameters(self):
        return [self.lookup_kwarg]

    def values(self):
        """
        Returns a list of values to filter on.
        """
        values = []
        value = self.used_parameters.get(self.lookup_kwarg, None)
        if value:
            values = value.split(',')
        # convert to integers if IntegerField
        if type(self.field.rel.to._meta.pk) in [IntegerField, AutoField]:
            values = [int(x) for x in values]

        return values

    def queryset(self, request, queryset):
        raise NotImplementedError

    def choices(self, cl):
        from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE
        yield {
            'selected': self.lookup_val is None,
            'query_string': cl.get_query_string({},
                [self.lookup_kwarg]),
            'display': _('All')
        }
        for pk_val, val in self.lookup_choices:
            selected = pk_val in self.values()
            pk_list = set(self.values())
            if selected:
                pk_list.remove(pk_val)
            else:
                pk_list.add(pk_val)
            queryset_value = ','.join([str(x) for x in pk_list])
            if pk_list:
                query_string = cl.get_query_string({
                    self.lookup_kwarg: queryset_value,
                    })
            else:
                query_string = cl.get_query_string({}, [self.lookup_kwarg])
            yield {
                'selected': selected,
                'query_string': query_string,
                'display': val,
            }

class IntersectionFieldListFilter(MultipleSelectFieldListFilter):
    """
    A FieldListFilter which allows multiple selection of
    filters for many-to-many type fields. A list of objects will be
    returned whose m2m contains all the selected filters.
    """

    def queryset(self, request, queryset):
        for value in self.values():
            filter_dct = {
                self.filter_statement: value
            }
            queryset = queryset.filter(**filter_dct)
        return queryset

class UnionFieldListFilter(MultipleSelectFieldListFilter):
    """
    A FieldListFilter which allows multiple selection of
    filters for many-to-many type fields. A list of objects will be
    returned whose m2m contains all the selected filters.
    """

    def queryset(self, request, queryset):
        filter_statement = "%s__in" % self.filter_statement
        filter_values = self.values()
        filter_dct = {
            filter_statement: filter_values
        }
        if filter_values:
            return queryset.filter(**filter_dct)
        else:
            return queryset
SalahAdDin commented 8 years ago

@f1nality this problem have some view with select2 logic, maybe?

@carlosfvieira, i think that it's a problem with the way that django jet builds the select inputs, it don't use normal select input, it use a fake select input.

SalahAdDin commented 8 years ago

Related: https://github.com/geex-arts/django-jet/issues/25

jordotech commented 7 years ago

bump

Ismael-VC commented 7 years ago

Please come to the django-jet Discord server so we can organize if you like:

Welcome! 😄

SalahAdDin commented 7 years ago

@jordotech Do you have this error?

DmitryGerasimov commented 6 years ago

Hi,

I also ran into this issue. Here is my code of template for a multi-select filter:

{% load i18n %}
<h3>{% blocktrans with filter_title=title %} By {{ filter_title }} {% endblocktrans %}</h3>
<ul>
    <li {% if spec.selected %}class="selected"{% endif %}>
        <div class="submit-row CA-MultiselectDropdown">
            <select multiple="multiple" style="width: 95%" class="CA-MultiselectDropdown__Select">
                {% for choice in choices %}
                    <option value="{{ choice.query_string }}" {% if choice.selected %}selected="selected"{% endif %}>{{ choice.display }}</option>
                {% endfor %}
            </select>
            <p class="help">{% trans 'Hold down "Control", or "Command" on a Mac, to select more than one.' %}</p>

            <p class="submit-row">
                <input style="width:47%" type="reset" value="{% trans "Clear" %}" data-reset-url="{{ spec.reset_url }}">
                <input style="width:47%" type="submit" value="{% trans "Search" %}" data-field-name="{{ spec.lookup_kwarg }}">
            </p>
        </div>
    </li>
</ul>

It works great without "django-jet". However, when used "django-jet" I get an empty select: image

As I understand it, this is due to this code: https://github.com/geex-arts/django-jet/blob/dev/jet/static/jet/js/src/layout-updaters/toolbar.js#L31

This line also raises many questions. It turns out, I can not add my custom filter, even a simple input field... https://github.com/geex-arts/django-jet/blob/dev/jet/static/jet/js/src/layout-updaters/toolbar.js#L108

I see that there is a specific work around the multiple selects, for example here: https://github.com/geex-arts/django-jet/blob/dev/jet/static/jet/js/src/features/filters.js#L11 https://github.com/geex-arts/django-jet/blob/dev/jet/static/jet/js/src/layout-updaters/toolbar.js#L42

And yet, what is the correct way to add a multi-select filter to django-jet?

Versions: Python 3.6.5 (default, May 5 2018, 03:07:21) [GCC 6.3.0 20170516] on linux Django==2.0.8 django-jet==1.0.7

Thanks, Dmitry

SalahAdDin commented 6 years ago

@f1nality

DmitryGerasimov commented 6 years ago

Hi guys,

Any news? Is there a chance to fix this issue?

Thanks, Dmitry