Opened 5 weeks ago

Last modified 5 weeks ago

#28933 new New feature

Implement a range-based filter for Django Admin date_hierarchy

Reported by: hakib Owned by: nobody
Component: contrib.admin Version: 2.0
Severity: Normal Keywords: date_hierarchy
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The predicate generated by date_hierarchy makes it very difficult for databases to optimize the query.

The following date hierarchy:


Will generate the following where clause (PostgreSql):

WHERE created between '2017-01-01' and '2017-31-12' and EXTRACT('month', created) = 12 and EXTRACT('day', created) = 16

The query above will not be able to utilize range based indexes on the date hierarchy column - on big tables this has a significant performance impact.

The current implementation of date hierarchy is relying on the "default" filtering mechinizem used by Django Admin. I propose implementing custom filtering for Django Admin that will better utilize it's hierarchical nature and make it more database "friendly".

Instead of the query above the date hierarchy would generate the following predicates for different levels of the heirarchy:

WHERE created >= '2017-12-16' and created < '2017-12-17'
WHERE created >= '2017-12-01' and created < '2018-01-01'
WHERE created >= '2017-01-01' and created < '2018-01-01'

I already [wrote about this issue]( and [published a package]( that implement the above as a custom SimpleListFilter.

I already have a PR ready - I'm following proper protocol here so please let me know if this is acceptable.

Change History (1)

comment:1 Changed 5 weeks ago by Simon Charette

Has patch: set
Triage Stage: UnreviewedAccepted
Note: See TracTickets for help on using tickets.
Back to Top