Open unformatt opened 5 years 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()
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