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.
* 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>
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>
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>
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>
* 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>
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>
* owner: Shreya Bamne => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:7>
* 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>
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>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:10>
* 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>
* 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>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:13>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:14>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:15>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32539#comment:16>
* 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>
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>
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>