How to change the Django admin filter to use a dropdown instead of list?
Solution 1:
Thanks @beholderrk, @gediminas and @jk-laiho! I packaged this into a reusable app.
Install:
pip install django-admin-list-filter-dropdown
Enable in settings.py
:
INSTALLED_APPS = (
...
'django_admin_listfilter_dropdown',
...
)
Use in admin.py
:
from django_admin_listfilter_dropdown.filters import (
DropdownFilter, ChoiceDropdownFilter, RelatedDropdownFilter
)
class EntityAdmin(admin.ModelAdmin):
...
list_filter = (
# for ordinary fields
('a_charfield', DropdownFilter),
# for choice fields
('a_choicefield', ChoiceDropdownFilter),
# for related fields
('a_foreignkey_field', RelatedDropdownFilter),
)
Here's what it looks like:
Solution 2:
I cannot comment answers so I'll add to beholderrk's answer here.
- create a new template called
dropdown_filter.html
or similar - copy the code of filter.html from feincms to
dropdown_filter.html
-
create a new filter class in
filters.py
:from django.contrib.admin.filters import AllValuesFieldListFilter class DropdownFilter(AllValuesFieldListFilter): template = 'admin/dropdown_filter.html'
-
now you can use this filter in your admin class:
class SomeAdmin(admin.ModelAdmin): # ... list_filter = (('country', DropdownFilter),)
Works great!