#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
------------------------------------------+------------------------
               Reporter:  Simon Willison  |          Owner:  nobody
                   Type:  Uncategorized   |         Status:  new
              Component:  contrib.admin   |        Version:  3.1
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32539>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.5dd945c2389871308cf5d26d24b79cda%40djangoproject.com.

Reply via email to