django-admin-rangefilter
django-admin-rangefilter is a Django application that enhances the admin UI by adding custom date, datetime, and numeric range filters. It is currently at version 0.13.5 and maintains an active release cadence, frequently updating for Django compatibility and bug fixes.
Warnings
- breaking Version 0.13.0 introduced a change to the default range template and added support for collapsible filters (Django >= 4.1). If you have custom templates overriding `rangefilter/date_filter.html` or similar, they might require adjustments to work with this new version and take advantage of new features.
- gotcha It's a common mistake to forget adding 'rangefilter' to your `INSTALLED_APPS` in `settings.py`. Without this, the custom filters will not be registered correctly and will not appear in the admin interface.
- gotcha Prior to version 0.13.3, there was a bug that prevented users from clearing filters properly. This could lead to confusing UX where applied filters could not be reset.
- gotcha The `collapsible` filter option, introduced in v0.13.0, is only supported for Django versions 4.1 and higher. Attempting to use it with older Django versions will not have the desired effect and might lead to unexpected rendering.
- gotcha In versions prior to 0.12.2 (specifically fixed in 0.12.1 and 0.12.2), `DateTimeRangeFilter` might have incorrectly handled microsecond precision for upper time bounds, potentially excluding valid entries or providing incorrect range filtering for `DateTimeField`s.
Install
-
pip install django-admin-rangefilter
Imports
- DateRangeFilter
from rangefilter.filters import DateRangeFilter
- DateTimeRangeFilter
from rangefilter.filters import DateTimeRangeFilter
- NumericRangeFilter
from rangefilter.filters import NumericRangeFilter
Quickstart
# settings.py
INSTALLED_APPS = [
# ...
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
# ...
'rangefilter',
# ...
]
# models.py (example)
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
created_at = models.DateField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
price = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
def __str__(self):
return self.name
# admin.py
from django.contrib import admin
from rangefilter.filters import DateRangeFilter, DateTimeRangeFilter, NumericRangeFilter
from .models import MyModel
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ('name', 'created_at', 'updated_at', 'price')
list_filter = (
('created_at', DateRangeFilter),
('updated_at', DateTimeRangeFilter),
('price', NumericRangeFilter),
# Add 'collapsible=True' for Django >= 4.1:
# ('created_at', {'filter_class': DateRangeFilter, 'collapsible': True}),
)