Django Admin List Filter Dropdown
django-admin-list-filter-dropdown is a Python library that enhances the Django administration interface by converting default list filters into dropdowns. This improves usability and reduces clutter in the sidebar, especially when a filter has numerous options. The current version is 1.0.3, released in 2019, suggesting a stable but less actively maintained project.
Warnings
- gotcha The library has not seen a new release since October 2019. While it generally works with modern Django versions, explicit compatibility with Django 4.x or 5.x is not guaranteed and may require manual testing or minor template adjustments. Consider testing thoroughly on newer Django projects.
- gotcha The dropdown filters typically only render as a dropdown if there are a sufficient number of distinct options (e.g., more than 2 or 3). If fewer options exist, Django's default list filter rendering (as a simple list) will likely be used instead.
- gotcha This library does not natively provide search/autocomplete functionality within the dropdowns. For filtering very large datasets, users might find it cumbersome to scroll through a long list.
- gotcha Forgetting to add `'django_admin_listfilter_dropdown'` to your `INSTALLED_APPS` in `settings.py` is a common mistake that will prevent the custom dropdown filters from being recognized and applied in the Django admin.
Install
-
pip install django-admin-list-filter-dropdown
Imports
- DropdownFilter
from django_admin_listfilter_dropdown.filters import DropdownFilter
- ChoiceDropdownFilter
from django_admin_listfilter_dropdown.filters import ChoiceDropdownFilter
- RelatedDropdownFilter
from django_admin_listfilter_dropdown.filters import RelatedDropdownFilter
Quickstart
# settings.py
INSTALLED_APPS = [
# ... other apps
'django_admin_listfilter_dropdown',
# ...
]
# admin.py
from django.contrib import admin
from django.db import models # For example model
from django_admin_listfilter_dropdown.filters import DropdownFilter, ChoiceDropdownFilter, RelatedDropdownFilter
# Example Models
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Product(models.Model):
STATUS_CHOICES = [
('in_stock', 'In Stock'),
('out_of_stock', 'Out of Stock'),
('coming_soon', 'Coming Soon'),
]
name = models.CharField(max_length=100)
status = models.CharField(max_length=20, choices=STATUS_CHOICES)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'status', 'category', 'is_active')
list_filter = (
('status', ChoiceDropdownFilter), # For fields with choices
('category', RelatedDropdownFilter), # For foreign key fields
('is_active', DropdownFilter), # For boolean fields, or custom SimpleListFilter
)
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name',)