Skip to main content

FacetsMixin

This class provides a mixin for calculating facet counts within a change list interface. It defines a framework for retrieving a filtered queryset and aggregating counts based on specific primary key attributes. Subclasses are required to implement the specific logic for facet count calculation by overriding the core aggregation method.

Methods


get_facet_counts()

@classmethod
def get_facet_counts(
pk_attname: str,
filtered_qs: [QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)
) - > dict

Calculates the specific counts for each facet option based on the provided queryset. Subclasses must implement this method to define how facet data is aggregated.

Parameters

NameTypeDescription
pk_attnamestrThe attribute name of the primary key used for counting unique records
filtered_qs[QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)The Django QuerySet containing the filtered records to be aggregated

Returns

TypeDescription
dictA dictionary mapping facet names to their calculated aggregate counts

get_facet_queryset()

@classmethod
def get_facet_queryset(
changelist: [ChangeList](../views/main/changelist.md?sid=django_contrib_admin_views_main_changelist)
) - > dict

Computes the final facet counts for a given changelist by aggregating data from a filtered queryset. It excludes current facet parameters from the filters to ensure all available options are counted correctly.

Parameters

NameTypeDescription
changelist[ChangeList](../views/main/changelist.md?sid=django_contrib_admin_views_main_changelist)The Django admin ChangeList instance providing the base queryset and request context

Returns

TypeDescription
dictA dictionary containing the aggregated facet counts for the admin changelist view