Opened 3 years ago
Last modified 3 years ago
#33931 assigned Cleanup/optimization
Optimize calling of `get_app_list` with AdminSites index/app_index
| Reported by: | Daniel Hahler | Owned by: | Daniel Hahler |
|---|---|---|---|
| Component: | contrib.admin | Version: | 4.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Roman | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | no | UI/UX: | no |
Description
AdminSite.index gets the list of available apps twice: for app_list and available_apps in the context.
I think this can be optimized to only call it once.
With app_index it could also be filtered in app_index itself.
The benefit of this is handling the use case of moving apps around in get_app_list (from custom_auth into auth, after calling the default method), which fails when the list is filtered by app_label before.
I think changing index makes sense, and is the far more often used code path, but changing app_index might break some projects, where by now it might be expected that get_app_list gets called with an app_label - and optimizing it there removed the internal use of calling get_app_list with app_label completely again. (This was changed in Django 4.1: https://code.djangoproject.com/ticket/7497)
Change History (3)
comment:1 by , 3 years ago
| Patch needs improvement: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 3 years ago
| Has patch: | set |
|---|
comment:3 by , 3 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Hey Daniel.
I think at least for the
indexcase it does make sense. My only reservation there is whether the micro-optimisation justifies the change in readability:get_app_list()is clear as day, whereas I might need to dig down to see why we're doing"app_list": context["available_apps"]. A comment there in the two cases would be enough I guess,Aliasing for the template context, (or not).The
app_indexcase is more complex. It looks like maybe theapp_labelusage could be teased out/simplified, but it's clearly old/delicate. I wonder how much effort you want to put in here? Do you want to deprecate the (4.1 added)app_labelargument, as unused?What about (then) simplifying
_build_app_dict()— thelabelparameter would be unused, and there's a block of code using that that could be removed? If we're going to do this, I think we should clean that up too?If you're happy to take that on, then happy to progress to see what others think, given that
_build_app_dict()is somewhat complex (to read at least). Otherwise we canwontfixas it's likely marginal.I'd imagine a bit of defensive programming would be enough here no? 🤔 But maybe there's a slightly separate thought/improvement there?