If, for a field that you want to filter by, you have more than ~10 values, the filtering sidebar starts to be ugly and harder to use.
I'm looking for a solution to replace the <li>
with a dropdown selection (combobox) or something similar that will solve the same problem.
Filter dropdowns provide a UI for applying filter criteria against columns in the GridView and its descendants, and against card fields in the CardView and LayoutView. You can choose between four filter dropdown presentation styles. The table below lists the main properties and methods related to this visual element.
Django-property-filter is an extension to django-filter and provides functionality to filter querysets by class properties. It does so by providing sub-classes for Filters and Filtersets to keep existing django-filter functionality. For more details and examples check the documentation.
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:
I cannot comment answers so I'll add to beholderrk's answer here.
dropdown_filter.html
or similardropdown_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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With