[Django] #32539: Support accessing the current filtered queryset from within SimpleListFilter.lookups()

17 views
Skip to first unread message

Django

unread,
Mar 11, 2021, 9:07:22 PM3/11/21
to django-...@googlegroups.com
#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.

Django

unread,
Mar 12, 2021, 1:42:23 AM3/12/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+------------------------------------

Reporter: Simon Willison | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* type: Uncategorized => New feature
* stage: Unreviewed => Accepted


Comment:

Tentatively accepted, it looks reasonable but I'm not sure if it's doable
with the current objects relations. Would you like to work on a patch?

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:1>

Django

unread,
Mar 12, 2021, 10:09:35 AM3/12/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+------------------------------------

Reporter: Simon Willison | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------

Comment (by Simon Willison):

I'd love to, but I'm a bit stuck on where to begin with this.

Like you, I'm skeptical that this is a simple bug fix. I think there are
two options here:

- A medium-sized change to the architecture of the admin, such that
`.lookups()` no longer gets called when the `ChangeList` is constructed.
I'm not at all sure how much work this would be or if it could be
implemented without breaking existing custom code.
- A new, optional mechanism for people who want to use
`model_admin.get_changelist_instance(request)` inside their `.lookups()`
method - potentially even defining a new `.lazy_options(...)` method that
people can implement which deliberately doesn't get called until later on,
to avoid this `RecursionError`

I'd like it if there was a third option, "just rewrite things in an
invisible way such that this works" - but my hunch is that's not possible.
And the two options I listed above both warrant some discussion with
people who are more familiar with the internals of the admin than I am!

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:2>

Django

unread,
Jun 28, 2021, 12:18:49 PM6/28/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+------------------------------------

Reporter: Simon Willison | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------

Comment (by Sarah Boyce):

I was wondering if this is being worked on or if I could try to pick it
up, happy to work on it in a group as well

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:3>

Django

unread,
Jun 29, 2021, 3:07:50 AM6/29/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+------------------------------------

Reporter: Simon Willison | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------

Comment (by Carlton Gibson):

Hi Sarah — you're very welcome to pick it up. 🙂

I'd suggest an initial time-boxed investigation to work out what the state
of play is.

At that point you'll be the world expert™ on this issue, so either you'll
have an idea or you'll want input. If you open a PR at that point (even
just adding comments in the second case) to explain what's going on, it's
easier for others to get up to speed and input.

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:4>

Django

unread,
Dec 2, 2021, 5:45:15 PM12/2/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+----------------------------------------
Reporter: Simon Willison | Owner: Shreya Bamne
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+----------------------------------------
Changes (by Shreya Bamne):

* owner: nobody => Shreya Bamne
* status: new => assigned


Comment:

I would like to try working on this ticket. I am not sure if someone is
already working on this. (Claiming the ticket for now. I am happy to
collaborate if someone is already working on this ticket. :))

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:5>

Django

unread,
Dec 3, 2021, 12:27:13 PM12/3/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+----------------------------------------
Reporter: Simon Willison | Owner: Shreya Bamne
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+----------------------------------------

Comment (by Shreya Bamne):

I have added comments in few files which would help to understand the
issue. I am not sure what next steps are to fix it and I am looking for
some input to begin working on this ticket. I hope the comments are
helpful.

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:6>

Django

unread,
Dec 15, 2021, 11:15:09 PM12/15/21
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+------------------------------------
Reporter: Simon Willison | Owner: (none)
Type: New feature | Status: new
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+------------------------------------
Changes (by Shreya Bamne):

* owner: Shreya Bamne => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:7>

Django

unread,
Jan 21, 2023, 1:47:14 PM1/21/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce

Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* owner: (none) => Sarah Boyce


* status: new => assigned

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/16495

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:8>

Django

unread,
Feb 2, 2023, 4:13:02 AM2/2/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------

Comment (by Carlton Gibson):

The PR here looks promising. With a
[https://github.com/carltongibson/django_ticket_32539 simple test project]
it correctly allows facetted (with counts) filtering.

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:9>

Django

unread,
Feb 3, 2023, 1:57:26 AM2/3/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:10>

Django

unread,
Feb 3, 2023, 9:59:25 AM2/3/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0


Comment:

Second point is in the PR now, I have tried to improve the documentation,
I think I would like to add a picture (as it will illustrate it better)
but I'm not sure where the images are coming from - is there a project
that is used for the documentation images?

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:11>

Django

unread,
Feb 9, 2023, 5:10:48 AM2/9/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


Comment:

See [https://github.com/django/django/pull/16495#issuecomment-1419546008
comment].

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:12>

Django

unread,
Feb 11, 2023, 6:08:28 PM2/11/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:13>

Django

unread,
Feb 23, 2023, 3:54:38 AM2/23/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:14>

Django

unread,
Feb 24, 2023, 7:14:47 AM2/24/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
--------------------------------+---------------------------------------
Reporter: Simon Willison | Owner: Sarah Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+---------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:15>

Django

unread,
Mar 3, 2023, 2:21:08 PM3/3/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
-------------------------------------+-------------------------------------

Reporter: Simon Willison | Owner: Sarah
| Boyce
Type: New feature | Status: assigned
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:16>

Django

unread,
Mar 4, 2023, 6:53:46 AM3/4/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
-------------------------------------+-------------------------------------
Reporter: Simon Willison | Owner: Sarah
| Boyce
Type: New feature | Status: closed
Component: contrib.admin | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"868e2fcddae6720d5713924a785339d1665f1bb9" 868e2fcd]:
{{{
#!CommitTicketReference repository=""
revision="868e2fcddae6720d5713924a785339d1665f1bb9"
Fixed #32539 -- Added toggleable facet filters to ModelAdmin.

Thanks Carlton Gibson, Simon Willison, David Smith, and Mariusz
Felisiak for reviews.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:17>

Django

unread,
Mar 5, 2023, 7:19:51 AM3/5/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
-------------------------------------+-------------------------------------
Reporter: Simon Willison | Owner: Sarah
| Boyce
Type: New feature | Status: closed
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"2660bc19f396b0aa74b11e00452ad39df0dba825" 2660bc1]:
{{{
#!CommitTicketReference repository=""
revision="2660bc19f396b0aa74b11e00452ad39df0dba825"
Refs #32539 -- Fixed facet filter tests on Oracle.

Follow up to 868e2fcddae6720d5713924a785339d1665f1bb9.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:18>

Django

unread,
Mar 24, 2023, 8:42:48 AM3/24/23
to django-...@googlegroups.com
#32539: Support accessing the current filtered queryset from within
SimpleListFilter.lookups()
-------------------------------------+-------------------------------------
Reporter: Simon Willison | Owner: Sarah
| Boyce
Type: New feature | Status: closed
Component: contrib.admin | Version: 3.1

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"cffcf0ef17b2dfd744d3bc64080229c1b500508f" cffcf0e]:
{{{
#!CommitTicketReference repository=""
revision="cffcf0ef17b2dfd744d3bc64080229c1b500508f"
Refs #32539 -- Fixed hide counts icon for RTL languages.

Bug in 868e2fcddae6720d5713924a785339d1665f1bb9.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:19>

Reply all
Reply to author
Forward
0 new messages