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 775 forks source link

SimpleListFilter not working #377

Open unformatt opened 5 years ago

unformatt commented 5 years ago

Typically, when using SimpleListFilter, you get a text box that you can enter search text into and hit enter to search. Jet is transforming this search box into a dropdown that does nothing. I can type into dropdown but it only filters what's in the dropdown (the dropdown is empty)...does not trigger a search i.e. form post to the server like it should:

Django 1.11

image

ljluestc commented 1 month ago

# admin_filters.py

from django.contrib.admin import SimpleListFilter
from django.utils.translation import gettext_lazy as _

class CustomSearchFilter(SimpleListFilter):
    title = _('Search')  # Title for the filter
    parameter_name = 'search'  # Query parameter name

    def lookups(self, request, model_admin):
        return []

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(**{self.parameter_name + '__icontains': self.value()})
        return queryset

    def choices(self, cl):
        # This method is overridden to render the custom filter HTML
        yield {
            'selected': self.value() == '',
            'query_string': cl.get_query_string({}, [self.parameter_name]),
            'display': _('All')
        }

    def form(self):
        from django import forms
        class FilterForm(forms.Form):
            search = forms.CharField(
                required=False,
                widget=forms.TextInput(attrs={'placeholder': _('Search...')})
            )
        return FilterForm()