Skip to content

Commit

Permalink
Added date based filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 20, 2018
1 parent 12525ff commit 03f2531
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/date_based_filtering.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
How to add date base filtering in Django admin?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

You can add a date based filtering on any date field by setting the :code:`date_hierarchy`.::

@admin.register(Hero)
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
...
date_hierarchy = 'added_on'


It looks like this:

.. image:: date_filtering.png

This can be very costly with a large number of objects. As an alternative, you can subclass :code:`SimpleListFilter`, and allow filtering only on years or the months.
1 change: 1 addition & 0 deletions heroes_and_monsters/entities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class HeroAdmin(admin.ModelAdmin, ExportCsvMixin):
list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent", "children_display")
list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)
actions = ["mark_immortal"]
date_hierarchy = 'added_on'

exclude = ['added_by',]

Expand Down

0 comments on commit 03f2531

Please sign in to comment.