Filtering by custom date range in Django admin Filtering by custom date range in Django admin django django

Filtering by custom date range in Django admin


In django 1.4, you can user list_filter. try:

from django.contrib.admin import DateFieldListFilterclass PersonAdmin(ModelAdmin):    list_filter = (        ('date_field_name', DateFieldListFilter),    )

This will give some built-in ranges, but it will work if you put the date range in url, like:

?date__gte=2009-5-1&date__lt=2009-8-1

If you need a date picker (like jquery), then you need to extend DateFieldListFilter. I sent a patch to django-admin-filtrate, so, check there soon.


Note: I wrote this answer in 2009, when the required functionality was not available in Django as a public API. For Django 1.4+, see the other answers.

This functionality isn't provided as far as I'm aware, but you can build it yourself.

Firstly, you can filter items using date__gte and date__lte as GET arguments in the url.

For example

/admin/myapp/bar/?date__gte=2009-5-1&date__lt=2009-8-1

will display all bar objects with dates in May, June or July 2009.

Then if you override the admin/change_list.html template file, you can add widgets for start and end dates, which navigate to the required url.


Hat tip to Daniel's answer to another SO question, which taught me about using queryset filter parameters as GET arguments.