﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32539	Support accessing the current filtered queryset from within SimpleListFilter.lookups()	Simon Willison	Sarah Boyce	"It would be really useful if code running inside a custom `SimpleListFilter.lookups()` method could access the filtered queryset for the current `ModelAdmin` instance, based on the current request.

Most significantly, this would allow admin filters to display counts for the number of items that would be returned if they were selected - for a classic faceted browse interface.

Sadly this isn't possible at the moment. I wrote about this in more detail here: https://til.simonwillison.net/django/almost-facet-counts-django-admin - but the short version is that the following:
{{{
class StateCountFilter(admin.SimpleListFilter):
    # ...
    def lookups(self, request, model_admin):
        changelist = model_admin.get_changelist_instance(request)
        qs = changelist.get_queryset(request)
        states_and_counts = qs.values_list(
            ""state__abbreviation"", ""state__name""
        ).annotate(n = Count('state__abbreviation'))
}}}
Raises a `RecursionError` because the `model_admin.get_changelist_instance(request)` method itself triggers a call to this `.lookups()` method."	New feature	closed	contrib.admin	3.1	Normal	fixed			Ready for checkin	1	0	0	0	0	0
