Opened 3 years ago
Last modified 7 weeks ago
#33095 closed Cleanup/optimization
Admin actions shown with zero results — at Version 1
Reported by: | Richard Laager | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Carlton Gibson | Triage Stage: | Unreviewed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description (last modified by )
contrib/admin/views/main.py has this:
# Admin actions are shown if there is at least one entry # or if entries are not counted because show_full_result_count is disabled self.show_admin_actions = not self.show_full_result_count or bool(full_result_count)
This was changed in 17557d068c43bd61cdc6c18caf250ffa469414a1 for #8408.
Is it intentional that the admin actions be shown if there are any rows in the table as opposed to any results on the page? Put differently, if there are no results on the page, I can't do anything with the actions, right?
It seems like this should use result_count (which is the number of filtered results) rather than full_result_count (which is the number of rows in the table):
# Admin actions are shown if there is at least one entry self.show_admin_actions = bool(result_count)
Additionally, if the above is accepted, then the only use of full_result_count is on the search page, which opens up another opportunity to optimize. As discussed further in #8408, there are two different SELECT COUNT(*) calls. One is for the paginator, which operates on the filtered results and one is for full_result_count which is unfiltered. The latter is only used once (if the above suggestion is adopted), in contrib/admin/templates/admin/search_form.html.
So we could skip calculating full_result_count if there is no search being done. Note that this is subtly different than if there are no filters; see #22810 for why that is an issue.
Alternatively, if you want to be safe and not make assumptions about what people might be doing with full_result_count, this also works: full_result_count = lazy(self.root_queryset.count()). Then if it is never evaluated, the query never runs. I've tested both approaches.