Use filter in your admin.py
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'
parameter_name = 'country'
autocomplete_queryset = Country.objects.all()
search_fields = ['name__icontains']
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 = [
(1, 'Active',),
(2, 'Suspended',),
(3, 'Deleted',),
]
@admin.register(Person)
class PersonAdmin(Select2AdminFilterMixin, admin.ModelAdmin):
list_filter = (CountryFilter, ProfessionFilter, StatusFilter,)