
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
django-select2-admin-filters
Advanced tools
A simple extension to Django app to render filters in django admin panel as autocomplete widget.
This extension is based on django-select2 and works with or without Grappelli.
Install using pip
pip install django-select2-admin-filters
Update INSTALLED_APPS, you need too put django_select2_admin_filters after admin and django_select2
INSTALLED_APPS = [
'django.contrib.admin',
#
'django_select2',
'django_select2_admin_filters',
]
Update urls.py to use model filters (refer to django-select2 documentation)
path('select2/', include('django_select2.urls')),
Filters are generally of two types, but each of them can be single or multiple selectable:
from django.contrib import admin
from django_select2_admin_filters.admin import (
Select2AdminFilterMixin)
from django_select2_admin_filters.filters import (
ChoiceSelect2Filter, MultipleChoiceSelect2Filter,
ModelSelect2Filter, MultipleModelSelect2Filter)
from your_app.models import Country, Person, Profession
class CountryFilter(ModelSelect2Filter):
title = 'Country of residence' # filter's title
parameter_name = 'country' # parameter used in url and by default field name of Foreign Key used to filter results
autocomplete_queryset = Country.objects.all() # queryset to autocomplete
search_fields = ['name__icontains'] # fields of Country model used to filtering
# optionally you can override queryset method
def queryset(self, request, queryset):
val = self.value()
if val:
return queryset.filter(country_of_residence=val)
return queryset
class ProfessionFilter(MultipleModelSelect2Filter):
title = 'Profession'
parameter_name = 'profession'
autocomplete_queryset = Profession.objects.all()
search_fields = ['name__icontains']
def queryset(self, request, queryset):
val = self.value_as_list()
if len(val) > 0:
return queryset.filter(professions__profession_id__in=val)
return queryset
class StatusFilter(ChoiceSelect2Filter):
title = 'Status'
parameter_name = 'status'
autocomplete_choice_list = [ # list of choices
(1, 'Active',),
(2, 'Suspended',),
(3, 'Deleted',),
]
@admin.register(Person)
class PersonAdmin(Select2AdminFilterMixin, admin.ModelAdmin):
# change_list_template = 'admin/change_list_filter_sidebar.html' <- DON'T override change_list_template
list_filter = (CountryFilter, ProfessionFilter, StatusFilter,) # actually you cannot mix filters with traditional filters
dependent_fields
FAQs
A simple extension to Django app to render filters in django admin panel as autocomplete widget.
We found that django-select2-admin-filters demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.