How to add a date filter to Django admin?
This tutorial is on How to add a date filter to Django admin. You can add a date filtering on any date field by sending the date_hierarchy. The date_hierarchy uses QuerySet.datetimes()
internally. The date_hierarchy to the name of a DateField or DateTimeField in your model.
date_hierarchy = 'pub_date'
And you can also specify a field on a related model using the __
.
date_hierarchy = 'author__pub_date'
Example
from django.contrib import admin from .models import Question @admin.register(Question) class QuestionAdmin(admin.ModelAdmin): date_hierarchy = 'pub_date' # DateField
It looks like this:
As an alternate, you can subclass SimpleListFilter to allow filtering only in years or months.
Many thanks for the simple and elegant solution.
Cheers!
You’re very welcome! I’m glad I could help. Cheers to your success! ?