Fixed #32539 -- Added toggleable facet filters to ModelAdmin.

Thanks Carlton Gibson, Simon Willison, David Smith, and Mariusz
Felisiak for reviews.
This commit is contained in:
sarahboyce 2023-02-16 13:23:24 +01:00 committed by Mariusz Felisiak
parent 50ca4defcb
commit 868e2fcdda
20 changed files with 568 additions and 38 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Before After
Before After

View file

@ -210,3 +210,14 @@ It is possible to specify a custom template for rendering a list filter::
See the default template provided by Django (``admin/filter.html``) for a
concrete example.
.. _facet-filters:
Facets
======
.. versionadded:: 5.0
By default, counts for each filter, known as facets, can be shown by toggling
on via the admin UI. These counts will update according to the currently
applied filters. See :attr:`ModelAdmin.show_facets` for more details.

View file

@ -1002,6 +1002,54 @@ subclass::
editing, or deleting an object. You can have filters cleared by setting
this attribute to ``False``.
.. attribute:: ModelAdmin.show_facets
.. versionadded:: 5.0
Controls whether facet counts are displayed for filters in the admin
changelist. Defaults to :attr:`.ShowFacets.ALLOW`.
When displayed, facet counts update in line with currently applied filters.
.. class:: ShowFacets
.. versionadded:: 5.0
Enum of allowed values for :attr:`.ModelAdmin.show_facets`.
.. attribute:: ALWAYS
Always show facet counts.
.. attribute:: ALLOW
Show facet counts when the ``_facets`` query string parameter is
provided.
.. attribute:: NEVER
Never show facet counts.
Set ``show_facets`` to the desired :class:`.ShowFacets` value. For example,
to always show facet counts without needing to provide the query
parameter::
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
...
# Have facets always shown for this model admin.
show_facets = admin.ShowFacets.ALWAYS
.. admonition:: Performance considerations with facets
Enabling facet filters will increase the number of queries on the admin
changelist page in line with the number of filters. These queries may
cause performance problems, especially for large datasets. In these
cases it may be appropriate to set ``show_facets`` to
:attr:`.ShowFacets.NEVER` to disable faceting entirely.
.. attribute:: ModelAdmin.radio_fields
By default, Django's admin uses a select-box interface (<select>) for